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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8085267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:20:22  来源:igfitidea点击:

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

objective-ciosuiimageassetsfilepath

提问by Ali

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?

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

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

采纳答案by Ali

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

回答by Randall

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

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

回答by giapnh

You also can use [myasset aspectRatioThumbnail]to get your image.

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