如何从 UIImagePickerController 获取缩略图或可保存路径以用于 UIImageView? [英] How do I get a thumbnail or saveable path from UIImagePickerController to use for a UIImageView?

查看:28
本文介绍了如何从 UIImagePickerController 获取缩略图或可保存路径以用于 UIImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Could somebody please explain or show some sample code of how I can use get a thumbnail to be put into a UIImageView after the user selects a photo with UIImagePickerController?

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

Let's say I want to set a thumbnail as the image of a table view cell. When the user presses the cell, the image picker is shown and the user selects an image that is already on their device. I also want to save the path to that thumbnail so that the next time the view is displayed, the proper thumbnail can be shown.

我能够显示图像选择器,并且我的代表正确获取了所选图像,但我想要缩略图的路径,并且我想使用该路径加载缩略图(即我想保存路径).我今天已经搜索了几个小时,但还没有弄清楚.也许我不了解 ALAsset,或者可能是别的东西,但我找不到任何对我有帮助的例子.我以前从未使用过图像选择器或 ALAsset,所以我对此完全陌生.

I am able to display an image picker and my delegate get's the chosen image correctly, but I want the path to the thumbnail, and I want to load the thumbnail using that path (i.e. I want to save the path). I've searched for hours today and haven't figured this out. Perhaps I'm not understanding ALAsset, or maybe it is something else, but I can't find any examples that are helping me. I have never used the image picker or ALAsset before now, so I'm completely new at this.

推荐答案

//如果你正在阅读这篇文章并看看下面的评论并认为??WTF??这是因为我正在编辑我的原始答案而不是发布两个答案,希望事情会更干净.重要的是要知道 ALAssetsLibrary 是 iOS 4.x 的东西.

//If you are reading this and look at the comments below and think ??WTF?? it is because I am editing my original Answer instead of posting two Answers in hopes that things will be cleaner. It is important to know that ALAssetsLibrary is an iOS 4.x thing.

下面的代码将用于获取资源库 URL,然后从缩略图表示中生成 UIImage.虽然我直接使用资产库 url,但没有理由不能通过将字符串表示形式转换为 NSURL 来满足 imageURL 分配的相同代码.免责声明:此代码可能会泄漏或更糟,但它回答了原始发布者的问题,并希望具有价值.

The code below will function to take an asset-library URL and then make a UIImage out of the thumbnail representation. Though I use the asset-library url directly there is no reason that this same code couldn't begin by transforming a string representation into a NSURL in order to satisfy the imageURL assignment. Disclaimer: this code probably leaks or something worse, but it answers the original poster's question and is hopefully of value.

下面的代码大量借鉴了 t他的 Stack Overflow问题 基本上涵盖了相同的主题.除了这里的代码,我还包含了 AssetsLibrary.framework 和另一个问题中引用的 ALAssetsLibrary 类型定义.

The code below is borrowing heavily on this Stack Overflow question that covers basically this same topic. In addition to the code here, I have included AssetsLibrary.framework and the ALAssetsLibrary typedefs referenced in the other question.

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

The whole trick is that you cannot reference the NSURL from an asset-library directly. I think (though I don't know) that it is somehow referencing a data store instead of a file so the data returned from the URL isn't straight NSData so you cannot use it the old way.

代码中有一个UIImageView,叫做photo.希望其他一切都很容易弄清楚.

There is a UIImageView in the code that is called photo. Hopefully everything else is easy to figure out.

- (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 块中的代码更改为如下所示:

If you don't want the thumbnail but do want the full photo, you will just change the code in the AssetForURLResult block to something like this:

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

我们可以把这个练习留给用户.

We can leave that exercize for the user.

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

Good luck, hope this helps clear things up for you.

这篇关于如何从 UIImagePickerController 获取缩略图或可保存路径以用于 UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆