3

我的相册中有一些图像。我已经检索了所有这些资产的 url。我想使用资产 url 获取特定的图像……下面的代码给了我资产 url。

    ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];

                      NSString *uti = [defaultRepresentation UTI];
                      NSURL *URL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];

现在使用这个 url,我怎样才能得到图像(UIImage)?

4

2 回答 2

10

通过这个获取Url,

NSURL *url= (NSURL*) [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];

并从 url 获取图像,如下所示。

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    @autoreleasepool {
        CGImageRef iref = [rep fullScreenImage];
        if (iref) {
            UIImage *image = [UIImage imageWithCGImage:iref];
            self.loadedImage = image;
            dispatch_async(dispatch_get_main_queue(), ^{
                //UIMethod trigger...
            });
            iref = nil;
        }
    } 
};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Can't get image - %@",[myerror localizedDescription]);
};

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:yourUrl
               resultBlock:resultblock
              failureBlock:failureblock];
于 2014-02-01T05:46:01.253 回答
0
UIImage *img = [UIImage imageWithCGImage:[[myAsset defaultRepresentation] fullResolutionImage]

希望这可以帮助

于 2014-02-01T05:12:43.180 回答