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

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

问题描述

我目前正在使用iOS上的Imgur通过以下代码将图像上传到服务器:

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"];

当我在模拟器中运行并从模拟器的图片库上传文件时,该代码可以正常工作,因为我处于快速以太网连接中.然而,选择与iPhone拍摄的图像时相同的代码超时在iPhone上.因此,我通过从网络上保存一幅小图像并尝试上传该图像来进行了尝试.

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.

这使我相信iPhone拍摄的大图像正在通过速度较慢的3G网络超时.在发送之前,是否可以通过iPhone压缩/调整图像大小?

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?

推荐答案

您应该可以通过执行类似的操作来制作较小的图像

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];

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

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

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

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