如何在上传到服务器之前在 iOS 上压缩/调整图像大小?
我目前正在使用 iOS 上的 Imgur 使用以下代码将图像上传到服务器:
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"];
在模拟器中运行并从模拟器的照片库上传文件时,代码运行良好,因为我使用快速以太网连接。但是,当选择用 iPhone 拍摄的图像时,相同的代码在 iPhone 上会超时。因此,我尝试从网络上保存一张小图像并尝试上传该图像,结果成功了。
这让我相信 iPhone 拍摄的大图像在速度较慢的 3G 网络上会超时。有什么方法可以在发送之前压缩 iPhone 中的图像/调整其大小吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
此代码片段将调整图像大小:
变量
newSize
是一个CGSize
,可以像这样定义:This snippet will resize the image:
The variable
newSize
is aCGSize
and can be defined like so:一个独立的解决方案:
感谢@Tuan Nguyen。
A self-contained solution:
Thanks to @Tuan Nguyen.
为了补充@Tuan Nguyen,这可能是最快、最优雅的方法。
链接到 John Muchow 的帖子iphonedevelopertips.com,向 UIImage 添加类别是一种非常非常方便的快速扩展方式。
只需调用
即可返回 NSDATA 的 640x480 表示图像(可能是在线图像),无需任何更多代码行。
To complement @Tuan Nguyen, this is maybe the fastest and most elegant way to do that.
To link to John Muchow's post at iphonedevelopertips.com , adding a category to a UIImage is a very very handy way to scale in a very fast fashion.
Just calling
returns you a 640x480 representation image of your NSDATA ( that could be an online image ) without any more line of code.
Matt Gemmell 的 MGImageUtilities 非常好,可以有效地调整大小并使用一些减少工作量的方法。
Matt Gemmell's MGImageUtilities are very nice, resizing efficiently and with some effort-reducing methods.
在此代码中 0.5 表示 50% ...
In this code 0.5 means 50% ...
使用这个简单的方法
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
use this simple method
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
Zorayr 函数的快速实现(进行了一些更改,以包括实际单位而非比例的高度或宽度约束):
Swift implementation of Zorayr's function (with a bit of a change to include height or width constraints by actual units not scale):
Swift 2.0 版本的 Jagandeep Singh 方法但需要将数据转换为图像,因为 NSData?不会自动转换 UIImage。
Swift 2.0 version of Jagandeep Singh method but need to convert data to image because NSData? is not converted UIImage automatically.
尝试按比例因子转换 JPG。这里我用的是0.5
就我而言:
原始 --- 图像大小(字节):85KB &之后 --- 图像大小(字节):23KB
try to convert JPG by scaling factor. Here I am using 0.5
In my case:
Original --- Size of Image(bytes): 85KB & After --- Size of Image(bytes): 23KB
这是调用它的方法
返回图像,该图像可以在任何地方显示......
This is the method calling
it returns image this image u can display any where...........
您应该能够通过执行类似的操作
(对于四分之一大小的图像)来制作较小的图像,然后将较小的图像转换为 PNG 或您需要的任何格式。
You should be able to make a smaller image by doing something like
(for a quarter-size image) then convert the smaller image to a PNG or whatever format you need.