如何在上传到服务器之前在iPhone OS SDK上压缩/调整图像大小? [英] How to compress/resize image on iPhone OS SDK before uploading to a server?

查看:145
本文介绍了如何在上传到服务器之前在iPhone OS SDK上压缩/调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NSData * imageData =使用Imgur在iOS上使用Imgur将图像上传到服务器。 UIImagePNGRepresentation(图像); 
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * fullPathToFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@SBTempImage.png];
[imageData writeToFile:fullPathToFile atomically:NO];

[uploadRequest setFile:fullPathToFile forKey:@image];

在模拟器中运行并从模拟器的照片库上传文件时,在快速以太网连接上。但是,当选择使用iPhone拍摄的图像时,相同的代码会在iPhone上超时。所以,我试图通过从网络上保存一个小图片,并尝试上传,这工作。

这使我相信iPhone拍摄的大图像是超时3G网络稍慢。有什么办法在发送之前从iPhone压缩/调整图像的大小?

谢谢!

解决方案

你应该可以做一个更小的图像

  UIImage * small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation]; 

(对于四分之一大小的图像),然后将较小的图像转换为PNG或任何您需要的格式。


I'm currently uploading an image to a server using Imgur on iOS with the following code:

NSData* imageData = UIImagePNGRepresentation(image);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* fullPathToFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"SBTempImage.png"];
[imageData writeToFile:fullPathToFile atomically:NO];

[uploadRequest setFile:fullPathToFile forKey:@"image"];

The code works fine when run in the simulator and uploading a file from the simulator's photo library because I'm on a fast ethernet connection. However, the same code times out on the iPhone when selecting an image taken with the iPhone. So, I tried it by saving a small image from the web and attempting to upload that, which worked.

This leads me to believe the large images taken by the iPhone are timing out over the somewhat slow 3G network. Is there any way to compress/resize the image from the iPhone before sending it?

Thanks!

解决方案

You should be able to make a smaller image by doing something like

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];

(for a quarter-size image) then convert the smaller image to a PNG or whatever format you need.

这篇关于如何在上传到服务器之前在iPhone OS SDK上压缩/调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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