【问题标题】:PHImageManager's method "requestImageDataForAsset" gives crash in SwiftPHImageManager 的方法“requestImageDataForAsset”在 Swift 中导致崩溃
【发布时间】:2015-07-18 05:37:12
【问题描述】:

我正在使用 PHImageManagers 的 requestImageDataForAsset 来计算相机胶卷中特定照片的内存大小。它在我的设备上运行良好,但一些用户在计算内存大小(通过分析和崩溃报告发现)后会发生崩溃。

code and crash report

请帮忙。

【问题讨论】:

  • @Vizllx 感谢您回复有关崩溃报告的任何想法?所有 iOS 版本都会发生这种情况。

标签: ios swift xcode6 phasset


【解决方案1】:

如果您使用相同的过程来获取图像元数据或内存大小,它在哪个 iOS 版本上崩溃,在 iOS 8.3 中对我来说似乎工作正常:-

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

我正在关注此链接以进行我的照片套件操作:- phimagemanagerios-8-photokit

【讨论】:

【解决方案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)
        }
    }
}

【讨论】:

    【解决方案3】:

    如果您想加载 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多