-
Notifications
You must be signed in to change notification settings - Fork 0
/
FavoriteImagesViewController.swift
141 lines (103 loc) · 4.95 KB
/
FavoriteImagesViewController.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
//
// FavoriteImagesViewController.swift
// InsTag
//
// Created by student8 on 29/12/14.
// Copyright (c) 2014 student8. All rights reserved.
//
import UIKit
class FavoriteImagesViewController : UIViewController,UICollectionViewDataSource{
var json: [[String: AnyObject]]!
var entries : NSDictionary!
@IBOutlet weak var collectionView: UICollectionView!
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("cellInFavorites", 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(300) as UIImageView
dispatch_async(dispatch_get_main_queue(), { () -> Void in
imageView.image = image
})
}
}
}
})
return cell
}
func fetchJsnData(){
let urljson1 = "https://api.instagram.com/v1/users/self/media/liked?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 viewDidLoad() {
super.viewDidLoad()
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
self.fetchJsnData()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.collectionView.reloadData()
})
})
}
override func viewDidAppear(animated: Bool) {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), { () -> Void in
self.fetchJsnData()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.collectionView.reloadData()
})
})
}
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
}
}