0

我想使用assetURL 获取PHAsset。我使用了以下代码:

NSURL *assetURL=[NSURL URLWithString:file.filePath];
        PHFetchResult *fetchResult=[PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
        PHAsset *asset= [fetchResult firstObject];

file.filePath 具有我从中获取的资产 url

[[PHImageManager defaultManager]
                             requestImageDataForAsset:asset
                             options:imageRequestOptions
                             resultHandler:^(NSData *imageData, NSString *dataUTI,
                                             UIImageOrientation orientation,
                                             NSDictionary *info)
                             {[uploadingFile setValue:[NSString stringWithFormat:@"%@",[info valueForKey:@"PHImageFileURLKey"]] forKey:@"filePath"];}

但我总是得到fetchResultas的价值nil。有人能告诉我我哪里出错了吗?是因为我传递了错误的 URL 类型吗?

4

1 回答 1

0

你可以使用这个:

 PHContentEditingInputRequestOptions * editOperations = [[PHContentEditingInputRequestOptions alloc]init];
    editOperations.networkAccessAllowed = NO;
    editOperations.canHandleAdjustmentData =^BOOL(PHAdjustmentData * data){
        return NO;
    };

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [asset requestContentEditingInputWithOptions:editOperations completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
            if (asset.mediaType & PHAssetMediaTypeImage) {
                BOOL  isDownLoading = ![info objectForKey:PHImageCancelledKey]
                && ![info objectForKey:PHImageErrorKey] && ![info objectForKey:PHImageResultIsDegradedKey];
                if (isDownLoading) {
                    mode_asset.mode_fileUrl = [contentEditingInput.fullSizeImageURL absoluteString];
                    mode_asset.mode_UTI = contentEditingInput.fullSizeImageURL.pathExtension;
                    mode_asset.mode_fileName = contentEditingInput.fullSizeImageURL.lastPathComponent;
                }
                if (mode_asset.mode_filePathBlock) {
                    mode_asset.mode_filePathBlock(mode_asset);
                }


            }
                //                dispatch_group_leave(group);

                //                dispatch_semaphore_signal(sema);

        }];
    });
于 2016-04-22T07:43:34.563 回答