简体   繁体   English

从swift 3中的数据转换图像大小时调整大小

[英]Image size is resized when convert it from data in swift 3

I want to save an image in database. 我想在数据库中保存图像。 Therefore I convert it to Data. 因此我将其转换为数据。 However during these steps the width and height of the image will change. 但是,在这些步骤中,图像的宽度和高度将发生变化。 It is increased in size. 它的尺寸增加了。

// Original Image Size
print("Original Image Size : \(capturedImage.size)") // Displays (320.0, 427.0)

// Convert to Data
var imageData: Data?
imageData = UIImagePNGRepresentation(capturedImage)

// Store imageData into Db.

// Convert it back
m_CarImgVw.image = UIImage(data: damageImage!.imageData!, scale: 1.0)
print("m_CarImgVw Image Size : \(m_CarImgVw.image.size)") // Displays (640.0, 854.0)

I do not want the imagesize to increase! 我不希望图像大小增加!

If it's originally an image from your assets, it's probably @2x, which means the size in pixels (real size) is double the size in pts (displayed size). 如果它最初是来自您资产的图像,则可能是@ 2x,这意味着像素(实际大小)的大小是pts(显示大小)的两倍。 So the image size isn't actually increasing, it was 640x854 before and after the transform. 因此图像大小实际上并没有增加,在变换之前和之后它是640x854。 It's just that before the OS automatically scaled it because it was named @2x. 这只是在操作系统自动缩放之前,因为它被命名为@ 2x。

To use the original image scale you can replace 1.0 with capturedImage.scale . 要使用原始图像比例,您可以使用capturedImage.scale替换1.0。

Your problem is in this line: 你的问题在于这一行:

m_CarImgVw.image = UIImage(data: damageImage!.imageData!, scale: 1.0)

Can you see it? 你能看见它吗?

Hint: It's in scale: 1.0 . 提示:它的scale: 1.0

It looks like your original image was Retina (or @2x), so it had scale 2.0. 看起来你的原始图像是Retina(或@ 2x),所以它有2.0。

So you should either put your original image scale ( damageImage.scale ) there, or if you're presenting image on the screen you should use UIScreen 's scale . 因此,您应该将原始图像比例( damageImage.scale )放在那里,或者如果您在屏幕上显示图像,则应使用UIScreen的比例

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

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