-2

I want to fetch the last photo taken in the photoLibrary and add it to an array which will be used in the UIActivivtyViewController. When the UIActivityViewController is presented, the image is not shown and it appears that the UIImage was never added to the array.

Here is what I have,

NSMutableArray *items = [[NSMutableArray alloc]init];
[items addObject:@"someText"];

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];


__block UIImage *shareImage;

PHImageRequestOptions* options = [[PHImageRequestOptions alloc]init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.version = PHImageRequestOptionsVersionUnadjusted;
options.synchronous = YES;


[[PHImageManager defaultManager] requestImageForAsset:lastAsset


    targetSize:PHImageManagerMaximumSize
    contentMode:PHImageContentModeDefault
    options:PHImageRequestOptionsVersionCurrent
    resultHandler:^(UIImage *result, NSDictionary *info) {

    dispatch_async(dispatch_get_main_queue(), ^{



    shareImage = result;
    [items addObject:shareImage];  });



}];

NSMutableArray *activityItems = [NSMutableArray arrayWithArray:items];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];

Any help will be appreciated. I am new to to iOS dev and programming all together and I have been having a very hard time with this one.

Thanks

Ro4ch
  • 850
  • 8
  • 21
  • 1
    Your code is very difficult to read like that. Please paste it back in and format it as you see it on the screen. – Ali Beadle Apr 03 '16 at 07:43
  • Also, you have not described specifically what is going wrong. – Ali Beadle Apr 03 '16 at 07:45
  • Ok, I adjusted the code and added more detail in the question. The UIImage isn't appearing when the UIActivityViewController is called. It appears the UIImage isn't being added to the array. Thanks for the reply. – Ro4ch Apr 03 '16 at 08:18

1 Answers1

1

the UIImage is actually added to your items array. at the time you create the activityItems array and present the UIActivityViewController the block that adds the UIImage might not be finished though. if you added the presenting logic into the block (after adding the image to the array) it should work!

- (void)yourMethod {
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:@"someText"];

    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
    PHAsset *lastAsset = [fetchResult lastObject];

    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.resizeMode = PHImageRequestOptionsResizeModeExact;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    options.version = PHImageRequestOptionsVersionUnadjusted;
    options.synchronous = YES;

    [[PHImageManager defaultManager] requestImageForAsset:lastAsset
                                               targetSize:PHImageManagerMaximumSize
                                              contentMode:PHImageContentModeDefault
                                                  options:PHImageRequestOptionsVersionCurrent
                                            resultHandler:^(UIImage *result, NSDictionary *info) {
                                                dispatch_async(dispatch_get_main_queue(), ^{
                                                    [items addObject:result];

                                                    [self presentActivityControllerWithItems:items];
                                                });
                                            }];
}

- (void)presentActivityControllerWithItems:(NSArray *)items {
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    [self presentViewController:activityViewController animated:YES completion:nil];
}
André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • This is exactly what I was thinking as well. Are you saying that I should the UIActivityViewController under the [items addObject:shareImage]; ?? This is what I did and it did not work. Thanks for the reply! – Ro4ch Apr 03 '16 at 08:13
  • Text (which it does with a UIImage. – Ro4ch Apr 03 '16 at 08:47
  • Andre, you have helped me greatly!! Thank you so much for the help. – Ro4ch Apr 03 '16 at 08:52