From 73b3cb58c313e66543a28912336d434233369d99 Mon Sep 17 00:00:00 2001 From: etudiant Date: Wed, 30 Apr 2014 18:35:16 +0200 Subject: [PATCH] Added FlickrPicture handling and displaying. --- Flikrkr.xcodeproj/project.pbxproj | 6 +++++ Flikrkr/._FlickPicture.h | Bin 0 -> 4096 bytes Flikrkr/._FlickPicture.m | Bin 0 -> 4096 bytes Flikrkr/FlickPicture.h | 28 ++++++++++++++++++++ Flikrkr/FlickPicture.m | 42 ++++++++++++++++++++++++++++++ Flikrkr/PictureViewController.h | 3 +++ Flikrkr/PictureViewController.m | 17 +++++++++--- 7 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 Flikrkr/._FlickPicture.h create mode 100644 Flikrkr/._FlickPicture.m create mode 100644 Flikrkr/FlickPicture.h create mode 100644 Flikrkr/FlickPicture.m diff --git a/Flikrkr.xcodeproj/project.pbxproj b/Flikrkr.xcodeproj/project.pbxproj index 6b314cd..74528b5 100644 --- a/Flikrkr.xcodeproj/project.pbxproj +++ b/Flikrkr.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 5A11DD2718F68646007CE97C /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5A11DD2518F68646007CE97C /* InfoPlist.strings */; }; 5A11DD2918F68646007CE97C /* FlikrkrTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A11DD2818F68646007CE97C /* FlikrkrTests.m */; }; 5A11DD3318F6871F007CE97C /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5A11DD3218F6871F007CE97C /* Storyboard.storyboard */; }; + EC056A7A19114FCF006EB10D /* FlickPicture.m in Sources */ = {isa = PBXBuildFile; fileRef = EC056A7919114FCF006EB10D /* FlickPicture.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -71,6 +72,8 @@ 5A11DD2618F68646007CE97C /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 5A11DD2818F68646007CE97C /* FlikrkrTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FlikrkrTests.m; sourceTree = ""; }; 5A11DD3218F6871F007CE97C /* Storyboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Storyboard.storyboard; sourceTree = ""; }; + EC056A7819114FCF006EB10D /* FlickPicture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FlickPicture.h; sourceTree = ""; }; + EC056A7919114FCF006EB10D /* FlickPicture.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FlickPicture.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -143,6 +146,8 @@ 5A11DD3218F6871F007CE97C /* Storyboard.storyboard */, 5A066DD518F68EA500A16494 /* PictureViewController.h */, 5A066DD618F68EA500A16494 /* PictureViewController.m */, + EC056A7819114FCF006EB10D /* FlickPicture.h */, + EC056A7919114FCF006EB10D /* FlickPicture.m */, 5A066DD818F68EE900A16494 /* ReaderView.h */, 5A066DD918F68EE900A16494 /* ReaderView.m */, 5A11DD1418F68644007CE97C /* Images.xcassets */, @@ -286,6 +291,7 @@ 5A066DD718F68EA500A16494 /* PictureViewController.m in Sources */, 5A11DD1018F68644007CE97C /* AppDelegate.m in Sources */, 5A066DDA18F68EE900A16494 /* ReaderView.m in Sources */, + EC056A7A19114FCF006EB10D /* FlickPicture.m in Sources */, 5A11DD0C18F68644007CE97C /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Flikrkr/._FlickPicture.h b/Flikrkr/._FlickPicture.h new file mode 100644 index 0000000000000000000000000000000000000000..a322a672c1658e237ec8a00e724e436dd91ab339 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^9H4T3G%bukK2%&hIX_n~v7jI)RWBs9qQo^XIX@*cFCD0$v?NW}!rIW-#K_Rx z(!vDIh*9on2#kinXb6mkz-S1JhQMeDjE2By2#kinXb6mkz-S1dh5)Fu3Bq6?7m|@# ztWcC%oL^d$oT`wPUzDwonOBmUSCW~Zmza}NsgRSJR-%xUlbDwc>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^9H4T3G%bukK2%&hIX_n~v7jI)RWBs9qQo^XIX@*cFCD0$v?NW}!rIW-#K_Rx z(!vDIh*9on2#kinXb6mkz-S1JhQMeDjE2By2#kinXb6mkz-S1dh5)Fu3Bq6?7m|@# ztWcC%oL^d$oT`wPUzDwonOBmUSCW~Zmza}NsgRSJR-%xUlbDwc> + +typedef struct +{ + double latitude; + double longitude; + int radius; +}FlickLocation; + +@interface FlickPicture : NSObject +@property (strong, nonatomic) NSString * pictureId; +@property (strong, nonatomic) NSNumber * farm; +@property (strong, nonatomic) NSString * server; +@property (strong, nonatomic) NSString * secret; +@property (strong, nonatomic) NSString * title; + +- (NSURL*) url; + ++ (NSArray*) picturesAroundLocation:(FlickLocation) location; +@end diff --git a/Flikrkr/FlickPicture.m b/Flikrkr/FlickPicture.m new file mode 100644 index 0000000..4bd0dab --- /dev/null +++ b/Flikrkr/FlickPicture.m @@ -0,0 +1,42 @@ +// +// FlickPicture.m +// Flikrkr +// +// Created by etudiant on 30/04/2014. +// Copyright (c) 2014 Oui. All rights reserved. +// + +#import "FlickPicture.h" + +#define kFlickrAPIKey @"045f3e02082b7820ff89bb17c38f1e5d" + +@implementation FlickPicture + +- (NSURL *)url +{ + NSString * urlString = [NSString stringWithFormat:@"http://farm%i.staticflickr.com/%@/%@_%@.jpg", self.farm.intValue, self.server, self.pictureId, self.secret]; + return [NSURL URLWithString:urlString]; +} + ++ (NSArray *)picturesAroundLocation:(FlickLocation)location +{ + NSString * urlString = [NSString stringWithFormat:@"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&lat=%f&lon=%f&radius=%i&format=json&nojsoncallback=1",kFlickrAPIKey, location.latitude, location.longitude, location.radius]; + NSURL * url = [NSURL URLWithString:urlString]; + NSData * data = [NSData dataWithContentsOfURL:url]; + + NSDictionary * jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + + NSMutableArray * pictures = [NSMutableArray arrayWithCapacity:[jsonData[@"photos"][@"photo"] count]]; + for (NSDictionary * currentPicture in jsonData[@"photos"][@"photo"]) { + FlickPicture * picture = [[FlickPicture alloc] init]; + picture.pictureId = currentPicture[@"id"]; + picture.server = currentPicture[@"server"]; + picture.secret = currentPicture[@"secret"]; + picture.farm = currentPicture[@"farm"]; + picture.title = currentPicture[@"title"]; + + [pictures addObject:picture]; + } + return [NSArray arrayWithArray:pictures]; +} +@end diff --git a/Flikrkr/PictureViewController.h b/Flikrkr/PictureViewController.h index 0867399..bb0725a 100644 --- a/Flikrkr/PictureViewController.h +++ b/Flikrkr/PictureViewController.h @@ -7,7 +7,10 @@ // #import +#import "FlickPicture.h" @interface PictureViewController : UIViewController +@property (assign, nonatomic) FlickLocation location; + @end diff --git a/Flikrkr/PictureViewController.m b/Flikrkr/PictureViewController.m index 07c2cbf..4eb3c97 100644 --- a/Flikrkr/PictureViewController.m +++ b/Flikrkr/PictureViewController.m @@ -12,6 +12,7 @@ @interface PictureViewController () @property (weak, nonatomic) IBOutlet ReaderView *readerView; +@property (strong, nonatomic) NSArray * pictures; @end @@ -30,6 +31,12 @@ { [super viewDidLoad]; // Do any additional setup after loading the view. + FlickLocation location; + location.latitude = 51.500773; + location.longitude = -0.12464; + location.radius = 1; + + self.pictures = [FlickPicture picturesAroundLocation:location]; self.readerView.delegate = self; } - (void) viewDidAppear:(BOOL)animated @@ -39,12 +46,16 @@ } - (int) numberOfPages { - return 5; + return self.pictures.count; } - (UIView *) pageAtIndex:(int)index { - NSString * imageName = [NSString stringWithFormat:@"%i.jpg",index]; - UIImage * image = [UIImage imageNamed:imageName]; + //NSString * imageName = [NSString stringWithFormat:@"%i.jpg",index]; + //UIImage * image = [UIImage imageNamed:imageName]; + FlickPicture * picture = self.pictures[index]; + NSData * imageData = [NSData dataWithContentsOfURL:picture.url]; + UIImage * image = [UIImage imageWithData:imageData]; + UIImageView * imageView = [[UIImageView alloc] initWithImage: image]; imageView.frame = self.readerView.bounds; imageView.contentMode = UIViewContentModeScaleAspectFit;