-2

我想将图像上传到服务器。我可以做到这一点,它适用于UIImage. 但是,我想上传当前显示在我的应用程序中的图像,该图像位于UIImageView.

我怎样才能通过,或将UIImageView图像临时存储在一个UIImage?我尝试了以下方法,但它不起作用。

UIImage *myImage = imageView.image; 
4

2 回答 2

2

我还不确定为什么直接访问图像属性对您不起作用,但是您也可以将图像视图渲染到位图上下文中,然后从上下文中创建一个新图像。代码将类似于:

#import <QuartzCore/QuartzCore.h>  // necessary for referencing CALayers
...
CGSize imageSize = imageView.bounds.size; 
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image.layer renderInContext:context];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

请参阅技术问答 QA1703 - UIKit 应用程序中的屏幕捕获

于 2012-08-16T18:46:01.983 回答
1

你有什么应该工作,你得到什么样的错误?或者你可以试试这个......

UIImage *image = [[UIImage alloc] initWithData:UIImagePNGRepresentation(imageView.image)];

甚至更好...

UIImage *image = [UIImage imageWithData:UIImagePNGRepresentation(imageView.image)];

但是,当 UIImageView 中已经有一个 UIImage 时,为什么还要分配一个 UIImage?只需直接使用 imageView.image ...

于 2012-08-16T19:44:23.440 回答