如何从UIImagePickerController获取缩略图或可保存路径以用于UIImageView?

时间:2011-01-11 21:49:19

标签: iphone uiimageview uiimage uiimagepickercontroller alasset

在用户使用UIImagePickerController选择照片后,有人可以解释或展示一些示例代码,说明如何使用缩放图放入UIImageView吗?

假设我想将缩略图设置为表格视图单元格的图像。当用户按下单元格时,将显示图像选择器,用户选择已在其设备上的图像。我还想保存该缩略图的路径,以便下次显示视图时,可以显示正确的缩略图。

我能够显示一个图像选择器,我的委托正确获取所选图像,但我想要缩略图的路径,我想使用该路径加载缩略图(即我想保存路径)。 我今天搜索了几个小时,但没想出来。也许我不理解ALAsset,或者它可能是其他东西,但我找不到任何帮助我的例子。我之前从未使用过图像选择器或ALAsset,所以我对此完全是新的。

4 个答案:

答案 0 :(得分:15)

//如果您正在阅读本文,请查看以下评论​​并思考?WTF ??这是因为我正在编辑我的原始答案而不是发布两个答案,希望事情会更清晰。重要的是要知道ALAssetsLibrary是一个iOS 4.x的东西。

下面的代码将用于获取资产库URL,然后从缩略图表示中创建UIImage。虽然我直接使用资产库url,但没有理由不能通过将字符串表示转换为NSURL来满足imageURL赋值,从而无法启动相同的代码。免责声明:此代码可能泄漏或更糟糕,但它回答了原始海报的问题,并且希望有价值。

下面的代码大量借用t his Stack Overflow question,基本上涵盖了同一主题。除了这里的代码,我还包括AssetsLibrary.framework和另一个问题中引用的ALAssetsLibrary typedef。

整个技巧是你无法直接从资产库中引用NSURL。我认为(虽然我不知道)它以某种方式引用数据存储而不是文件,因此从URL返回的数据不是直接的NSData,因此您不能以旧的方式使用它。

代码中有一个名为photo的UIImageView。希望其他一切都很容易理解。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSLog(@"%@",imageURL);
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        CGImageRef iref = [myasset thumbnail];
        if (iref) {
            UIImage *theThumbnail = [UIImage imageWithCGImage:iref];
            [[self photo] setImage:theThumbnail];

        }
    };


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

    if(imageURL)
    {
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:imageURL 
                       resultBlock:resultblock
                      failureBlock:failureblock];
    }

    [self dismissModalViewControllerAnimated:YES];
}

如果您不想要缩略图但想要完整的照片,您只需将AssetForURLResult块中的代码更改为以下内容:

       ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];

我们可以为用户留下锻炼。

祝你好运,希望这有助于为你解决问题。

答案 1 :(得分:2)

我现在已经明白了。我遇到的困难是理解Blocks,主要是Blocks的语法使得很难理解它们是什么以及它们是如何工作的。

在UIImagePickerController的委托中,当用户选择了图像时,您可以使用以下命令获取资源库中图片的路径:

NSURL *path = [info objectForKey:UIImagePickerControllerReferenceURL];

使用该对象,您可以使用以下代码获取完整尺寸的图像或缩略图:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        // Gets the full size image
        self.fullSizeImage = [UIImage imageWithCGImage:iref];
    }

    // Gets the thumbnail
    self.thumbnail = [UIImage imageWithCGImage:[myasset thumbnail]];
};

ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
    NSLog(@"in failureblock, got an error: %@",[myerror localizedDescription]);
};

ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib assetForURL:urlForImage resultBlock:resultblock failureBlock:failureblock];

这个页面真的帮助我理解了块:Blocks in iOS 4
另请参阅display image from URL retrieved from ALAsset in iPhone

答案 2 :(得分:1)

保存网址可能会有问题:

  1. 至少在iOS 4.x中,URL不保证是唯一的。如果用户删除/添加图片,则可以重复使用它们,这显然很常见。因此,您保留的网址可能会在未来指向不同的图片!
  2. 当用户更新到iOS 5时,网址不再有效。啊!!! Apple重写了Photo Library,没有向后兼容性。

答案 3 :(得分:0)

您可以将从imagePicker中选择的图像保存到文件目录中,该目录是从选择器引用它的 以下是可用于保存图像的代码。在用户从选择器中选择照片时调用的回调方法中实现此方法

 NSFileManager   fileManager    = [NSFileManager defaultManager];

    NSArray*  output_PDF_paths  =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString* output_PDF_documentsDirectory = [output_PDF_paths objectAtIndex:0];
    NSString* output_PDF_ABSOLUTE_PATH  = [output_PDF_documentsDirectory stringByAppendingPathComponent:@"ImageName.png"];  

    BOOL success_new = [UIImagePNGRepresentation(ProfilePhotoselected) writeToFile:previewPagePath atomically:YES];

//这里的“ProfilePhotoselected”是你在callabck方法中作为参数从picker获得的图像