NSMutableArray 在将 UIImage 添加到数组后返回 nil
NSMutableArray returning nil after adding a UIImage to the array
我想获取 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 开发和编程的新手,我在这方面遇到了很多困难。
谢谢
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];
}
我想获取 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 开发和编程的新手,我在这方面遇到了很多困难。
谢谢
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];
}