将图像从文件路径加载到资产库的UIImage

Ali*_*Ali 20 assets objective-c filepath uiimage ios

涉及到我刚才的问题在这里.

我有资源库中图像的路径,例如:

assets-library://asset/asset.JPG?id=1000000001&ext=JPG
Run Code Online (Sandbox Code Playgroud)

现在,我如何将此路径中的图像加载到UIImage对象?

这是代码:

NSString *path = [occasion imagePath];

//temp
NSLog(@"the 2nd occasion imagePath is: %@", path);
//end

if (path != nil)
{
    //UIImage *image = [UIImage imageNamed:path];
    UIImage *image = [UIImage imageWithContentsOfFile:path];

    image = [image imageByScalingAndCroppingForSize:CGSizeMake(36.0, 42.0)];
    [[cell imageView] setImage:image];
}
Run Code Online (Sandbox Code Playgroud)

Ali*_*Ali 18

从我在上面的评论中链接的答案:

    -(void)findLargeImage
{
    NSString *mediaurl = [self.node valueForKey:kVMMediaURL];

    //
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            largeimage = [UIImage imageWithCGImage:iref];
            [largeimage retain];
        }
    };

    //
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
    };

    if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION])
    {
        [largeimage release];
        NSURL *asseturl = [NSURL URLWithString:mediaurl];
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:asseturl 
                       resultBlock:resultblock
                      failureBlock:failureblock];
    }
}
Run Code Online (Sandbox Code Playgroud)


Ran*_*all 13

请尝试以下方法: (UIImage *)imageWithContentsOfFile:(NSString *)path