Search code examples
iosobjective-cxcodeuiimagephphotolibrary

NSMutableArray returning nil after adding a UIImage to the array


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


Solution

  • 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];
    }