Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 624
Ali Avatar asked Nov 10 '11 19:11

Ali


People also ask

How do I find my UIImage path?

Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.

How do you add a UIImage in Objective C?

UIImage *img = [UIImage imageNamed:@"anyImageName"];

What is a UIImage?

An object that manages image data in your app.

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


2 Answers

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];
    }
}
like image 148
Ali Avatar answered Oct 23 '22 22:10

Ali


Try the following: (UIImage *)imageWithContentsOfFile:(NSString *)path

like image 22
Randall Avatar answered Oct 23 '22 23:10

Randall