-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTagViewController.swift
151 lines (117 loc) · 5.44 KB
/
SearchTagViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// SearchTagViewController.swift
// InsTag
//
// Created by student8 on 29/12/14.
// Copyright (c) 2014 student8. All rights reserved.
//
import UIKit
class SearchTagViewController : UIViewController,UICollectionViewDataSource, UISearchBarDelegate{
var json: [[String: AnyObject]]!
var entries : NSDictionary!
var tags: [String]!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var collectionView: UICollectionView!
@IBAction func followButtonClicked(sender: AnyObject) {
if searchBar.text != "" {
tags.append(searchBar.text)
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(tags, forKey: "tags")
}
searchBar.text = ""
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(json == nil){
return 0;
}
else{
return json.count
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellInSearch", forIndexPath: indexPath) as UICollectionViewCell
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
let cellInfo = self.json[indexPath.item]
if let img : AnyObject = cellInfo["images"]{
let images = img as [String:AnyObject]
if let lowres : AnyObject = images["low_resolution"]{
let lowresolution = lowres as [String:AnyObject]
if let urltemp : AnyObject = lowresolution["url"]{
let url = urltemp as String
let nsurl = NSURL(string: url)
let nsdata = NSData(contentsOfURL: nsurl!)
let image = UIImage(data: nsdata!)
let imageView = cell.viewWithTag(200) as UIImageView
dispatch_async(dispatch_get_main_queue(), { () -> Void in
imageView.image = image
})
}
}
}
})
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
let userDefaults = NSUserDefaults.standardUserDefaults()
if let defaultItems = userDefaults.arrayForKey("tags") {
tags = defaultItems as [String]
} else {
tags = [String]()
userDefaults.setObject(tags, forKey: "tags")
}
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
self.fetchJsnData(self.searchBar.text)
NSLog("selected tag is = %@",self.searchBar.text)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.collectionView.reloadData()
NSLog("%s","aa")
})
})
}
func fetchJsnData(key : String){
let urljson1 = "https://api.instagram.com/v1/tags/"+key+"/media/recent?access_token=583685231.1fb234f.ddba925fe0dd43089d69d6c414657ddb&count=35"
let nsurljson = NSURL(string: urljson1)
let nsdata = NSData(contentsOfURL: nsurljson!)
entries = NSJSONSerialization.JSONObjectWithData(nsdata!, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary
if let jsn : AnyObject = entries["data"]{
json = jsn as [[String: AnyObject]]
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let ImageVC = segue.destinationViewController as ImageViewController
let cell = sender as UICollectionViewCell
let indexPath = collectionView.indexPathForCell(cell)
let cellInfo = json[indexPath!.item]
if let img : AnyObject = cellInfo["images"]{
let images = img as [String:AnyObject]
if let lowres : AnyObject = images["low_resolution"]{
let lowresolution = lowres as [String:AnyObject]
if let urltemp : AnyObject = lowresolution["url"]{
let urlImage = urltemp as String
ImageVC.url = urlImage
}
}
}
let user = cellInfo["user"]! as [String: String]
let urlUserImage = user["profile_picture"]
let userName = user["username"]
var captionText = ""
if let cap : AnyObject = cellInfo["caption"]{
let caption = cap as [String:AnyObject]
if let capText:AnyObject = caption["text"]{
captionText = capText as String }
}
// let nsurl = NSURL(string: url!)
// let nsdata = NSData(contentsOfURL: nsurl!)
// let image = UIImage(data: nsdata!)
// let imageView = cell.viewWithTag(10) as UIImageView
// imageView.image = image
ImageVC.userImageUrl = urlUserImage
ImageVC.userName = userName
ImageVC.captionText = captionText
}
}