【问题标题】:How to fetch PHAsset from assetURL in Photos framework?如何从 Photos 框架中的assetURL 获取 PHAsset?
【发布时间】:2015-11-12 06:40:03
【问题描述】:

我想使用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"];}

但我总是将fetchResult 的值设为nil。有人能告诉我我哪里出错了吗? 是不是因为我传递了错误的 URL 类型?

【问题讨论】:

    标签: ios photosframework phasset phphotolibrary


    【解决方案1】:

    你可以用这个:

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

    【讨论】:

    • 我发现一个问题,调用这个方法会出问题,这个方法会阻塞UI,等待阻塞回调,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多