简体   繁体   English

iphone图像调整大小

[英]iphone image resizing

I have doubts about the resizing of images in my iphone app. 我怀疑在我的iPhone应用程序中调整图像大小。 So the user takes a picture or choses one from the gallery and then this pic is sent to a server. 因此,用户从图库中拍摄照片或选择一张照片,然后将该照片发送到服务器。 I already have the code from this answer to resize the pic: 我已经有了这个答案的代码来调整图片大小:

UIImage * small=[self imageByScalingToSize:CGSizeMake(150,150) img:image];

The size is just an example. 尺寸只是一个例子。

I send small to the server but I have to things to take into account: 我发送小到服务器,但我必须考虑到的事情:

  • The image has to be less or equal to 150 kilobytes, and 图像必须小于或等于150千字节,并且
  • I have read here that when the user sends images over 3G the iphone automatically resizes the image to (800,600). 我在这里看到,当用户通过3G发送图像时,iphone会自动将图像调整为(800,600)。

So what I would like to do is to resize image to (800,600) (this part I already have) and then check if it is more than 150k and somehow reduce it if it is. 所以我想做的是将图像调整为(800,600)(这部分我已经有),然后检查它是否超过150k,如果是的话以某种方式减少它。

Is there a way to calculate the number of bytes of the image knowing the size and maybe some other data ? 有没有办法计算图像的字节数知道大小和其他一些数据?
How could I achieve this size reduction in means of file size ? 如何通过文件大小减少这种尺寸?

convert your UIImage to NSData and user -length to get the byte size of the image. 将您的UIImage转换为NSData和user -length以获取图像的字节大小。

from apple documentation: 来自苹果文档:

length - Returns the number of bytes contained in the receiver. length - 返回接收器中包含的字节数。

you can convert the UIImage using either 你可以使用其中任何一个来转换UIImage

NSData * UIImagePNGRepresentation ( UIImage *image );

or 要么

NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality );

How are you uploading you image. 你是如何上传你的形象的? You should just be sending up the byte data, iOS will not know that it's an image and will not be able to resize it! 你应该只是发送字节数据,iOS不知道它是一个图像,将无法调整它的大小! Have you looked at using something like 你有没有看过像这样的东西?

NSData *imageData = UIImageJPEGRepresentation(myUIImage, compressionQuality);

To compress your image as well as resize it. 压缩图像以及调整图像大小。 This will give you an NSData object which you can the base64 encode and send you to your server. 这将为您提供一个NSData对象,您可以对base64进行编码并将其发送到您的服务器。 Also form the NSData you will be bale to see how big the the image is in bytes and determine if it is small enough. 同样形成NSData,您将看到图像有多大(以字节为单位)并确定它是否足够小。

Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM