Search code examples
iphoneobjective-ciosuiimagepickercontrollersocial-framework

IOS Social framework and image post


I am experimenting with the Social Framework. I was wondering if there is any possible way to attach the last photo taken and currently saved in the camera roll using this implementation from my app:

 - (IBAction)shareByActivity:(id)sender {
    NSArray *activityItems;

    if (self.sharingImage != nil) {
        activityItems = @[self.sharingImage, self.sharingText, self.addURL];
    } else {
        activityItems = @[self.sharingText, self.addURL];
    }

    UIActivityViewController *activityController =
    [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                      applicationActivities:nil];

    [self presentViewController:activityController
                       animated:YES completion:nil];
}

If so how do I modify the shareImage name in this specific portion of my -(void)viewDidLoad ?

self.sharingImage = [UIImage imageNamed:@"notSureWhatToPutHere"];

Everything works well, the social panel opens and has all the needed service. My only request is to find out how to call the latest image from the camera roll. I am not sure if I need the image real name or if there is any other way to achieve what i would like: a post with a picture attached (most recent picture in camera roll).

Any help is appreciated.


Solution

  • You can pretty painlessly get the last image in the camera roll using the asset library framework. Give this a try:

    #import <AssetsLibrary/AssetsLibrary.h>
    

    Then to get the image:

    ALAssetsLibrary *cameraRoll = [[ALAssetsLibrary alloc] init];
    [cameraRoll enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *images, BOOL *stop) {
        [images setAssetsFilter:[ALAssetsFilter allPhotos]];
        [images enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[images numberOfAssets] - 1] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
            if (result) {
                ALAssetRepresentation *rep = [result defaultRepresentation];
                self.sharingImage = [UIImage imageWithCGImage:[rep fullScreenImage]];
            }
        }];
    
    
    }
                            failureBlock: ^(NSError *error) {
                                NSLog(@"An error occured: %@",error);
                            }];