从UIImagePickerControllerReferenceURL加载UIImage [英] Loading UIImage from UIImagePickerControllerReferenceURL

查看:1441
本文介绍了从UIImagePickerControllerReferenceURL加载UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UIImagePickerController来允许用户从图像库中选择图像。然后我想在sqlite数据库中启动该文件的位置,以便稍后可以参考。

I am using a UIImagePickerController to allow users to select an image from the image library. I then want to start the location of that file in a sqlite database so I can refer to it later.

我一直在做一些关于如何做到这一点的谷歌搜索,而且我的确很短。我知道我可以通过调用委托方法来获取项目的ReferenceURL:

I've been doing some googling about how to do this and I'm coming up rather short. I know I can get the ReferenceURL of the item by calling inside the delegate method:

    NSURL *picURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"]

这确实给了我一个有效的NSURL对象,如果我输出picURL.absoluteString我可以什么似乎是文件的资产库位置的有效URL。但是,如果我尝试使用此url创建NSData对象,以便我可以依次创建UIImage,则代码将失败,因为NSData返回nil。例如:

This does give me a valid NSURL object and if I output picURL.absoluteString I can what appears to be a valid url to the assets-library location of the file. However, if I try to create an NSData object with this url so I can in turn create a UIImage, the code fails because NSData returns nil. For example:

    NSURL *picURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"]
    NSString *stringUrl = picURL.absoluteString;
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringUrl]];
    imgMain.image = [UIImage imageWithData:data]

我意识到我在某种程度上这里重复,但重点是获取我可以存储在sqlite中的URL的字符串表示,然后使用所述字符串创建一个图像(所以上面的代码代表了那个思路)。

I realize I'm being somewhat repetitive here but the point is to grab a string representation of the URL that I can store in sqlite and then use said string later to create an image (so the above code represents that train of thought).

我发现一些关于使用ALAssetsLibrary的信息,但这给了我各种各样的适合,因为它似乎不适合iOS 5和ARC。我不断收到错误消息'Receiver type ALAsset for instance message是一个前向声明。我可以在引用ALAsset / ALAssetsLibrary的文件上设置属性以忽略停止构建错误的ARC,但是一旦我离开代码块,数据就会消失。见下文:

I found some information floating around about using ALAssetsLibrary but that gave me all kinds of fits as it doesn't appear to play nice with iOS 5 and ARC. I keep getting error messages 'Receiver type ALAsset for instance message is a forward declaration. I can set properties on the file that references ALAsset / ALAssetsLibrary to ignore ARC which stops the build error but then as soon as I leave the code block the data goes away. See below:

    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    __block UIImage *returnValue = nil;
    [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
        returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
    } failureBlock:^(NSError *error) {
       // error handling
    }];
    [library release];

returnValue确实返回一个有效的图像但是一旦代码退出resultBlock,数据的内容就是消失(即returnValue再次变为null)。

returnValue does return a valid image but as soon as the code exits the resultBlock the contents of the data are gone (i.e. returnValue becomes null again).

所以我的问题是:如何使用UIImagePickerControllerReferenceURL并将其转换为我可以存储在sqlite中的字符串,然后使用所述字符串在a中创建有效的UIImage对象以后的时间?

So my question is: how do I use the UIImagePickerControllerReferenceURL and convert it to a string I can store in sqlite and then use said string to create a valid UIImage object at a later time?

推荐答案

看起来你正走在正确的轨道上,以保存资产网址。

Looks like you're on the right track, for saving the asset url.

Save
NSURL *picURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"]
NSString *stringUrl = picURL.absoluteString;


Reload AssetURL
NSURL asssetURL = [NSURL URLWithString:stringUrl]

至于稍后加载资产,我将基于你没有使用弧的假设。这可以解释为什么你的returnValue是零。

As for loading the asset later on, i'm going to base on the assumption you're not using arc. Which would explain why you're returnValue is nil.

您是否尝试过为结果添加保留?例如:

Have you tried adding a retain to the result? for example:

    NSURL *referenceURL = [NSURL URLWithString:savedAssetURLString]
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    __block UIImage *returnValue = nil;
    [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
        returnValue = [[UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]] retain]; //Retain Added
    } failureBlock:^(NSError *error) {
       // error handling
    }];
    [library release];

这篇关于从UIImagePickerControllerReferenceURL加载UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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