Fullsized image loading.

This commit is contained in:
Ash Furrow
2013-10-15 20:35:34 -04:00
parent 100325779b
commit 8012bfc7e4
6 changed files with 92 additions and 10 deletions

View File

@@ -8,8 +8,12 @@
#import <Foundation/Foundation.h>
@class FRPPhotoModel;
@interface FRPPhotoImporter : NSObject
+(RACSubject *)importPhotos;
+(RACSubject *)fetchPhotoDetails:(FRPPhotoModel *)photoModel;
@end

View File

@@ -11,15 +11,18 @@
@implementation FRPPhotoImporter
+(NSURLRequest *)urlRequest {
+(NSURLRequest *)popularURLRequest {
return [AppDelegate.apiHelper urlRequestForPhotoFeature:PXAPIHelperPhotoFeaturePopular resultsPerPage:100 page:1 photoSizes:PXPhotoModelSizeThumbnail sortOrder:PXAPIHelperSortOrderRating except:PXPhotoModelCategoryNude];
}
+(NSURLRequest *)photoURLRequest:(FRPPhotoModel *)photoModel {
return [AppDelegate.apiHelper urlRequestForPhotoID:photoModel.identifier.integerValue];
}
+(RACSubject *)importPhotos {
RACSubject *subject = [RACSubject subject];
NSURLRequest *request = [self urlRequest];
NSURLRequest *request = [self popularURLRequest];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
@@ -43,18 +46,76 @@
return subject;
}
+(RACSubject *)fetchPhotoDetails:(FRPPhotoModel *)photoModel {
RACSubject *subject = [RACSubject subject];
NSURLRequest *request = [self photoURLRequest:photoModel];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
id results = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil][@"photo"];
[self configurePhotoModel:photoModel withDictionary:results];
[self downloadFullsizedImageForPhotoModel:photoModel];
[subject sendNext:photoModel];
[subject sendCompleted];
}
else {
[subject sendError:connectionError];
}
}];
return subject;
}
+(void)configurePhotoModel:(FRPPhotoModel *)photoModel withDictionary:(NSDictionary *)dictionary {
// Basics details fetched with the first, basic request
photoModel.photoName = dictionary[@"name"];
photoModel.identifier = dictionary[@"id"];
photoModel.photographerName = dictionary[@"user"][@"username"];
photoModel.rating = dictionary[@"rating"];
photoModel.thumbnailURL = [dictionary[@"image_url"] firstObject];
photoModel.thumbnailURL = [self urlForImageSize:3 inDictionary:dictionary[@"images"]];
// Extneded attributes fetched with subsequent request
if (dictionary[@"comments_count"]) {
photoModel.fullsizedURL = [self urlForImageSize:4 inDictionary:dictionary[@"images"]];
}
}
+(NSString *)urlForImageSize:(NSInteger)size inDictionary:(NSDictionary *)dictioary {
/*
images = (
{
size = 3;
url = "http://ppcdn.500px.org/49204370/b125a49d0863e0ba05d8196072b055876159f33e/3.jpg";
}
);
*/
return [[[[[dictioary rac_sequence] filter:^BOOL(NSDictionary *value) {
return [value[@"size"] integerValue] == size;
}] map:^id(id value) {
return value[@"url"];
}] array] firstObject];
}
+(void)downloadThumbnailForPhotoModel:(FRPPhotoModel *)photoModel {
NSAssert(photoModel.thumbnailURL, @"Thumbnail URL must not be nil");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photoModel.thumbnailURL]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
photoModel.thumbnailData = data;
}];
}
+(void)downloadFullsizedImageForPhotoModel:(FRPPhotoModel *)photoModel {
NSAssert(photoModel.fullsizedURL, @"Full sized URL must not be nil");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photoModel.fullsizedURL]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
photoModel.fullsizedData = data;
}];
}
@end

View File

@@ -11,6 +11,7 @@
@interface FRPPhotoModel : NSObject
@property (nonatomic, strong) NSString *photoName;
@property (nonatomic, strong) NSNumber *identifier;
@property (nonatomic, strong) NSString *photographerName;
@property (nonatomic, strong) NSNumber *rating;
@property (nonatomic, strong) NSString *thumbnailURL;

View File

@@ -11,6 +11,10 @@
// Model
#import "FRPPhotoModel.h"
// Utilities
#import "FRPPhotoImporter.h"
#import <SVProgressHUD.h>
@interface FRPPhotoViewController ()
// Private assignment
@@ -35,7 +39,7 @@
return self;
}
- (void)viewDidLoad
-(void)viewDidLoad
{
[super viewDidLoad];
@@ -47,14 +51,22 @@
RAC(imageView, image) = [RACObserve(self.photoModel, fullsizedData) map:^id(id value) {
return [UIImage imageWithData:value];
}];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
self.imageView = imageView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[SVProgressHUD show];
// Fetch data
[[FRPPhotoImporter fetchPhotoDetails:self.photoModel] subscribeError:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:@"Error"];
} completed:^{
[SVProgressHUD dismiss];
}];
}
@end

View File

@@ -5,6 +5,7 @@ target "FRP" do
pod 'ReactiveCocoa', '2.1'
pod 'libextobjc', '0.3'
pod '500px-iOS-api', '1.0.4'
pod 'SVProgressHUD', '0.9'
end

View File

@@ -91,16 +91,19 @@ PODS:
- ReactiveCocoa/Core (2.1):
- ReactiveCocoa/no-arc
- ReactiveCocoa/no-arc (2.1)
- SVProgressHUD (0.9)
DEPENDENCIES:
- 500px-iOS-api (= 1.0.4)
- libextobjc (= 0.3)
- ReactiveCocoa (= 2.1)
- SVProgressHUD (= 0.9)
SPEC CHECKSUMS:
500px-iOS-api: fde71ddd4a05f06b94b8ec7c29bbcd2e589b7e70
libextobjc: 820a79dbbbc498611e04fffd07d2d52a5588e7ac
libffi: 64ef39353e747bb2b25e1026afd96a157bf9231c
ReactiveCocoa: b686fa735c28c2dc7311fbd2f0895b4d4ae07360
SVProgressHUD: 03d4845ec8e64591726428a08236c6a5489d45f8
COCOAPODS: 0.25.0
COCOAPODS: 0.26.2