2011-11-10 24 views
20

与我之前的问题here相关。从资源库的文件路径将图像加载到UIImage中

我在资产库中的图像的路径,例如:

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

现在,我怎么会从这个路径加载镜像到一个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]; 
} 

回答

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

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

0

您还可以使用[myasset aspectRatioThumbnail]让你图片。

相关问题