-2

我想获取在 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 开发和编程的新手,我一直很难用这个。

谢谢

4

1 回答 1

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];
}
于 2016-04-03T08:02:38.797 回答