Showing posts with label ALAsset. Show all posts
Showing posts with label ALAsset. Show all posts

Monday, August 11, 2014

Post UIImage on Fan Page as Admin using Facebook iOS SDK - GraphPath, FBRequestConnection, ALAsset

// Model 
//  FBPagesModel.h
@interface FBPagesModel : NSObject

@property (nonatomic, strong) NSString *access_token;
@property (nonatomic, strong) NSString *category;
@property (nonatomic, strong) NSString *pageId;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *perms;

- (id)initWithDictionary:(NSDictionary *)dictionary;


//  FBPagesModel.m
#import "FBPagesModel.h"

@implementation FBPagesModel

- (id)initWithDictionary:(NSDictionary *)dictionary {
    if (self = [super init]) {
        self.access_token = [dictionary objectForKey:@"access_token"];
        self.category     = [dictionary objectForKey:@"category"];
        self.pageId       = [dictionary objectForKey:@"id"];
        self.name         = [dictionary objectForKey:@"name"];
    }
    return self;
}

@end


// Uploader 
FBPagesModel *pageModel =  ....
Facebook *fb = [Facebook new];
[fb setAccessToken:pageModel.access_token];

ALAsset *asset =  ... // localMedia asset;
        UIImage *largeImage = [self imageForAsset:asset];

        // Capture a photo via file upload as multipart/form-data then use the source parameter:
        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                largeImage, @"source", nil];
        NSLog(@"Params ->  \n %@", params);
        /* make the API call */

NSString *graphPath = [NSString stringWithFormat:@"/%@/photos", pageModel.pageId];

[FBRequestConnection startWithGraphPath:graphPath
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  /* handle the result */
                                  if (!error) {
                                      NSLog(@"Uploaded to Page \n %@", result);
                                  } else {
                                      NSLog(@"Error posing on FB page \n %@", error);
                                  }
                              }];


- (UIImage *)imageForAsset:(ALAsset*)asset {
    ALAssetRepresentation * representation = [asset defaultRepresentation];
    
    CGImageRef image = [representation fullResolutionImage];
    UIImageOrientation orientation = (UIImageOrientation)[representation orientation];
    CGFloat scale = [representation scale];
    
    return [UIImage imageWithCGImage:image scale:scale orientation:orientation];

}