-
Notifications
You must be signed in to change notification settings - Fork 0
/
PictureViewController.m
142 lines (109 loc) · 3.54 KB
/
PictureViewController.m
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
//
// PictureViewController.m
// helloworld
//
// Created by Guo Huang on 6/27/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "PictureViewController.h"
@interface PictureViewController ()
@end
@implementation PictureViewController
@synthesize scrollView;
const CGFloat kScrollObjHeight = 480.0;
const CGFloat kScrollObjWidth = 320.0;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self getPhotoData];
[self propulateImages];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setScrollView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) propulateImages
{
scrollView.pagingEnabled = TRUE;
//NSLog(@"%@", photoURLsLargeImage);
if(photoURLsLargeImage !=nil){
for (int i=0; i< photoURLsLargeImage.count; i++) {
NSData *imageData = [photoURLsLargeImage objectAtIndex:i];
//NSLog(@"%@", imageData);
UIImage *img = [[UIImage alloc] initWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[scrollView addSubview:imageView];
}
[self layoutScrollImages];
}
}
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView setContentSize:CGSizeMake((photoURLsLargeImage.count * kScrollObjWidth), [scrollView bounds].size.height)];
}
- (void) getPhotoData{
NSURL *url = [NSURL URLWithString:@"http://www.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&group_id=51035641679@N01&format=json&nojsoncallback=1&api_key=2aa4ef551c23385d2fadab02c534f77f"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@", error);
if (json !=nil) {
//NSLog(@"%@", json);
int i = 0;
//NSLog(@"%@", [[json objectForKey:@"photos"] objectForKey:@"photo"] );
for (NSDictionary *photo in [[json objectForKey:@"photos"] objectForKey:@"photo"] ){
//NSLog(@"%@", [item objectForKey:@"id"]);
NSString *photoURLString =
[NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg",
[photo objectForKey:@"farm"], [photo objectForKey:@"server"],
[photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
i++;
[self getPictureById: photoURLString];
if(i == 10)
break;
}
}
}
- (void) getPictureById: (NSString *) pictureUrl
{
if(photoURLsLargeImage == nil)
photoURLsLargeImage = [[NSMutableArray alloc] init];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: pictureUrl]];
[photoURLsLargeImage addObject: data];
}
@end