如何上传从UIImagePickerController中获取的图像 [英] How to upload image that was taken from UIImagePickerController

查看:118
本文介绍了如何上传从UIImagePickerController中获取的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户使用 UIImagePickerController 从iPhone库中选择图像后,我想使用 ASIHTTPRequest 将其上传到我的服务器图书馆。

After user choose image from the iPhone library with UIImagePickerController, I want to upload it to my server using ASIHTTPRequest library.

我知道用 ASIHTTPRequest 我可以用文件的URl上传文件,但是如何获取图像网址是什么?

I know that with ASIHTTPRequest I can upload a file with the file's URl, but how do I get the image URL?

我知道我可以得到图片 UIImagePickerControllerReferenceURL ,如下所示:

I know I can get the image UIImagePickerControllerReferenceURL, that look like this:

"assets-library://asset/asset.JPG?id=F2829B2E-6C6B-4569-932E-7DB03FBF7763&ext=JPG"

这是我需要使用的网址吗?

is this the URL I need to use?

推荐答案

有两种方式

1:

您可以使用 imagePickerController 委托上传图片

You can upload the image using the imagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{   

    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    //Upload your image
}

2:

您可以保存已挑选的图片网址并稍后使用此

You can save the picked image url and upload later using this

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSString *imageUrl = [NSString stringWithFormat:@"%@",[info valueForKey:UIImagePickerControllerReferenceURL]];
    //Save the imageUrl
}

-(void)UploadTheImage:(NSString *)imageUrl{

 NSURL *url = [[NSURL alloc] initWithString:imageUrl];
 typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
 typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);    

 ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){

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

  if (ref) {
      myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];

        //upload the image   
     }      
  };      

  ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){

  };          


  ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
 [assetslibrary assetForURL:url resultBlock:result block failureBlock:failureblock];    

}

注意: 使用ARC.Better将 ALAssetsLibrary 对象用作对象时,请确保 ALAssetsLibrary 对象的范围单身。

Notes: Please make sure the scope of ALAssetsLibrary object while using ARC.Better to use the ALAssetsLibrary object as a singleton.

这篇关于如何上传从UIImagePickerController中获取的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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