在向阵列添加UIImage后,NSMutableArray返回nil

时间:2016-04-03 07:30:09

标签: ios objective-c xcode uiimage phphotolibrary

我想获取photoLibrary中拍摄的最后一张照片,并将其添加到将在UIActivivtyViewController中使用的数组中。当呈现UIActivityViewController时,图像不会显示,并且看起来UIImage从未添加到数组中。

这就是我所拥有的,

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

任何帮助将不胜感激。我是iOS开发人员和编程人员的新手,我一直很难用这个。

由于

1 个答案:

答案 0 :(得分:1)

UIImage实际上已添加到您的items数组中。在您创建activityItems数组并显示UIActivityViewController时,添加UIImage的块可能无法完成。如果您将呈现逻辑添加到块中(在将图像添加到数组之后)它应该可以工作!

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