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

查看:457
本文介绍了如何从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中创建UIImage。缩略图表示。尽管我直接使用资产库网址,但没有理由不能将相同的代码通过将字符串表示形式转换为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 typedef。

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.

整个窍门是,您无法从资产图书馆直接。我认为(尽管我不知道)它以某种方式引用了数据存储而不是文件,因此从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,称为照片。希望其他所有内容都很容易弄清。

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天全站免登陆