我正在使用PHImageManagers的requestImageDataForAsset来计算相机胶卷中特定照片的内存大小。它在我的设备中工作正常,但是一些用户在计算内存大小后会出现数量崩溃(通过分析和崩溃报告找到)。
请帮忙。
答案 0 :(得分:2)
如果您正在使用相同的过程来获取图像元数据或内存大小,那么在iOS 8.3中崩溃的iOS版本似乎运行正常: -
--------------------------------------------------------------
-- get the UIImage instance from a PHAsset
--------------------------------------------------------------
- (UIImage*)grabImageFromAsset:(PHAsset *)asset
{
__block UIImage *returnImage;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:CGSizeMake(200, 200)
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:
^(UIImage *result, NSDictionary *info) {
returnImage = result;
}];
return returnImage;
}
--------------------------------------------------------------
-- get the metadata of the image from a PHAsset
--------------------------------------------------------------
- (NSDictionary *)grabImageDataFromAsset:(PHAsset *)asset
{
__block NSMutableDictionary *imageAssetInfo = [NSMutableDictionary new];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:asset
options:options
resultHandler:
^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
if ([info[@"PHImageResultIsInCloudKey"] isEqual:@YES]) {
// in the cloud
NSLog(@"in the cloud (sync grabImageDataFromAsset)");
}
imageAssetInfo = [info mutableCopy];
if (imageData) {
imageAssetInfo[@"IMAGE_NSDATA"] = imageData;
}
}];
return imageAssetInfo;
}
我正在关注此照片套件操作的链接: - phimagemanager和ios-8-photokit
答案 1 :(得分:2)
当用户使用iCloud照片库将照片存储在iCloud中时,我遇到了类似的问题。我能够通过使调用异步来解决问题。看起来像这样
let options = PHImageRequestOptions()
options.synchronous = false
options.networkAccessAllowed = true
这是整个功能。
func getImageDataFromAsset(asset: PHAsset, completion: (data: NSData?) -> Void) {
let manager = PHImageManager.defaultManager()
let options = PHImageRequestOptions()
options.networkAccessAllowed = true
options.synchronous = false
manager.requestImageDataForAsset(asset, options: options) { (result, string, orientation, info) -> Void in
if let imageData = result {
completion(data: imageData)
} else {
completion(data: nil)
}
}
}
答案 2 :(得分:0)
如果你想加载你在Photos.app中拥有的所有照片而你不想要iCloud。你可以这样做:
该示例适用于集合视图。
@interface GalleryViewModel ()
@property (strong, nonatomic) NSMutableArray<PHAsset *> *assets;
@property (strong, nonatomic) PHImageManager *imageManager;
@property (strong, nonatomic) PHImageRequestOptions *requestOptions;
@property (strong, nonatomic) NSMutableArray<UIImage *> *imagesList;
@end
@implementation GalleryViewModel
- (instancetype) initWithContext:(ITXAppContext *)context {
self = [super initWithContext:context];
if (self) {
_assets = [[NSMutableArray alloc] init];
_imageManager = [PHImageManager defaultManager];
_requestOptions = [[PHImageRequestOptions alloc] init];
_imagesList = [[NSMutableArray alloc] init];
}
return self;
}
#pragma mark - Public methods
// ==================================================================================
// Public methods
- (void) viewModelDidLoad {
[self obtainAllPhotos];
}
#pragma mark - Private methods
// ==================================================================================
// Private methods
- (void) obtainAllPhotos {
self.requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
self.requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
self.requestOptions.synchronous = YES;
self.requestOptions.networkAccessAllowed = NO;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
__weak GalleryViewModel *weakSelf = self;
[result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[weakSelf.assets addObject:obj];
if (idx >= ([result count] - 1)) {
[weakSelf.viewDelegate setupView];
}
}];
}
#pragma mark - Get data from object
// ==================================================================================
// Get data from object
- (NSInteger) sizeGallery {
if (self.assets) {
return [self.assets count];
}
return 0;
}
- (UIImage *) imagesFromList:(NSInteger) index {
__block UIImage *imageBlock;
[self.imageManager requestImageForAsset:[self.assets objectAtIndex:index] targetSize:CGSizeMake(200, 200) contentMode:PHImageContentModeAspectFit options:self.requestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if (result) {
imageBlock = result;
}
}];
return imageBlock;
}
@end