2012-02-02 62 views
1

在我的应用程序中,用户可以拍摄图片或从图库中选择一个,然后以小图像视图显示。我需要将生成的图像url保存到数据库中,然后使用该URL将图像发送到服务器(只有当没有可用的Internet连接时才这样做)。这一切工作我有这样的代码:iphone图像选择器结果网址

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

NSURL *url=[info objectForKey:@"UIImagePickerControllerReferenceURL"]; 
NSLog(@"%@",url); 

//(.....) 
} 

当我保存的网址数据库中,我只是把它当作好像它是一个字符串参数和它的作品,我已经在数据库中查找的URL是有形式:

资产库://asset/asset.JPG ID = 5A900026-5994-4F93-8A49-D2C96BDA2659 & EXT = JPG

我的问题是,当我尝试使用这个网址既可以得到图像或获取数据,它会给我错误或空对象:

NSData *dataForFile =[[NSData alloc] initWithContentsOfURL:[url path]]; 
UIImage *img=[[UIImage alloc] initWithContentsOfFile:[url path]]; 

URL路径= asset.JPG /怪....

我也试过这样:

NSData *dataForFile =[[NSData alloc] initWithContentsOfURL:url]; 
UIImage *img=[[UIImage alloc] initWithContentsOfFile:url]; 

,但它也不起作用。我只需要其中一个数据对象或一个图像(从图像我可以获取数据),我怎么能这样做?非常感谢

回答

2

我相信你需要使用assetForURL:resultBlock:failureBlock:。我没有测试过,但我确定这是你需要的,因为那是一个资产URL(你需要链接ALAssetsFramework到项目目标)。

然后,您可以从ALAsset获取图像数据。

编辑:这应该得到从给定的URL中的全分辨率图像(如果该URL仍然有效):

__block UIImage *image=nil; 
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){ 
    ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
    image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; 
}; 
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ 
    NSLog(@"Cannot get image - %@",[myerror localizedDescription]); 
    // 
}; 
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; 
[assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock]; 
NSLog(@"%@", image); 

我在一些项目中使用几乎相同的代码,但我避风港” t测试了这一个。

编辑2:今天进行了几项测试。看起来这些块是异步运行的,这意味着在调用[assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock];后图像将不可用。相反,您可以使用resultblock中的图像。还有没有更多的理由宣布image作为block变量,因此代码将变为:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){ 
    ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
    UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; 
    #warning todo: use image here, or call a method and pass 'image' to it 
}; 
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){ 
    NSLog(@"Cannot get image - %@",[myerror localizedDescription]); 
}; 
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; 
NSURL *url=[info objectForKey:UIImagePickerControllerReferenceURL]; 
[assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock]; 
+0

THKS它看起来有点复杂。幸运的是我找到了一个教程:http://www.icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/ – vallllll 2012-02-02 15:11:56

+0

它不是很复杂,除了它使用块。我更新了一些我认为可以正常工作的代码。 – 2012-02-02 15:24:30

+0

对我不起作用我把它放在 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}方法和我使用从信息dic得到的url,但图像保持空所以我想它在iphone 5中不起作用,或者我失去了一些东西 – vallllll 2012-02-02 16:33:05