简体   繁体   中英

Load an image to UIImage from a file path to the asset library

Related to my previous question here .

I have a path to an image in the asset library , for example:

assets-library://asset/asset.JPG?id=1000000001&ext=JPG

Now, how could I load the image from this path to a UIImage object?

Here is the code:

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

from the answer linked in my comment above:

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

请尝试以下操作: (UIImage *)imageWithContentsOfFile:(NSString *)path

您还可以使用[myasset aspectRatioThumbnail]来获取图像。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM