调整 UIImage 大小的最简单方法?
在我的 iPhone 应用程序中,我用相机拍照,然后我想将其大小调整为 290*390 像素。我正在使用这种方法来调整图像大小:
UIImage *newImage = [image _imageScaledToSize:CGSizeMake(290, 390)
interpolationQuality:1];
它工作得很好,但它是一个未记录的功能,所以我不能再在 iPhone OS4 上使用它。
那么...调整 UIImage 大小的最简单方法是什么?
In my iPhone app, I take a picture with the camera, then I want to resize it to 290*390 pixels. I was using this method to resize the image :
UIImage *newImage = [image _imageScaledToSize:CGSizeMake(290, 390)
interpolationQuality:1];
It works perfectly, but it's an undocumented function, so I can't use it anymore with iPhone OS4.
So... what is the simplest way to resize an UIImage ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
调整 UIImage 大小的另一种方法是仅更改分辨率:
Yet another way of resizing an UIImage, by just changing the resolution:
斯威夫特2.0:
Swift 2.0 :
这是我的有点冗长的 Swift 代码
Here my somewhat-verbose Swift code
斯威夫特 4 答案:
Swift 4 answer:
最简单的方法是设置
UIImageView
的框架并将contentMode
设置为调整大小选项之一。或者,如果您确实需要调整图像大小,则可以使用此实用方法:
示例用法:
The simplest way is to set the frame of your
UIImageView
and set thecontentMode
to one of the resizing options.Or you can use this utility method, if you actually need to resize an image:
Example usage:
适用于 iOS 10+ 的正确 Swift 3.0 解决方案:使用
ImageRenderer
和闭包语法:这是 Objective-C 版本:
Proper Swift 3.0 for iOS 10+ solution: Using
ImageRenderer
and closure syntax:And here's the Objective-C version:
这是 Paul Lynch 的答案 的 Swift 版本,
作为扩展:
Here's a Swift version of Paul Lynch's answer
And as an extension:
适用于 Swift 4 和 iOS 10+ 的更紧凑版本:
用法:
A more compact version for Swift 4 and iOS 10+:
Usage:
用于拉伸填充、纵横比填充和纵横比贴合的 Swift 解决方案
,使用时您可以执行以下操作:
感谢 abdullahselek 提供的原始解决方案。
Swift solution for Stretch Fill, Aspect Fill and Aspect Fit
and to use you can do the following:
Thanks to abdullahselek for his original solution.
我也看到过这样做(我在
UIButtons
上使用它来表示正常和选定状态,因为按钮不会调整大小
以适应)。功劳归于原作者。首先创建一个名为
UIImageResizing.h
和UIImageResizing.m
的空 .h 和 .m 文件,将该 .h 文件包含在您要使用该函数的任何 .m 文件中然后这样称呼它:
I've also seen this done as well (which I use on
UIButtons
for Normal and Selected state since buttons don'tresize
to fit). Credit goes to whoever the original author was.First make an empty .h and .m file called
UIImageResizing.h
andUIImageResizing.m
Include that .h file in whatever .m file you're going to use the function in and then call it like this:
Paul 代码的这一改进将为您在配备视网膜显示屏的 iPhone 上提供清晰的高分辨率图像。否则缩小时会很模糊。
This improvement to Paul's code will give you a sharp high res image on an iPhone with a retina display. Otherwise when scaling down it's blurry.
这是一个简单的方法:
resizedImage是一个新图像。
Here is a simple way:
resizedImage is a new image.
这是对上面 iWasRobbed 编写的类别的修改。它保持原始图像的纵横比而不是扭曲它。
Here's a modification of the category written by iWasRobbed above. It keeps the aspect ratio of the original image instead of distorting it.
使用 iOS 15 或更高版本时,您可以使用
UIImage
的新prepareThumbnail
方法:更多信息请参见此处:
https://developer.apple.com/documentation/uikit/uiimage/3750845-preparethumbnail
When using iOS 15 or newer, you can use the new
prepareThumbnail
method ofUIImage
:More info here:
https://developer.apple.com/documentation/uikit/uiimage/3750845-preparethumbnail
为什么这么复杂?我认为使用系统 API 可以达到相同的结果:
唯一的问题是您应该传递目标比率的倒数作为第二个参数,因为根据文档,第二个参数指定原始图像与新缩放图像相比的比率。
Why so complicated? I think using system API can achieve the same result:
The only gotcha is you should pass inverse of target ratio as the second argument, as according to the document the second parameter specifies the ratio of original image compared to the new scaled one.
对于斯威夫特 5:
For Swift 5:
如果您只想缩小图像而不关心确切尺寸:
将比例设置为
0.25f
将为您提供来自 8MP 相机的 816 x 612 图像。这里为那些需要的人提供UIImage+Scale 类别。
If you just want an image smaller and don't care about exact size:
Setting scale to
0.25f
will give you a 816 by 612 image from a 8MP camera.Here's a category UIImage+Scale for those who needs one.
这是一个与 Swift 3 和 Swift 4 兼容的 UIImage 扩展,它可以将图像缩放到给定的尺寸和纵横比
示例用法
This is an UIImage extension compatible with Swift 3 and Swift 4 which scales image to given size with an aspect ratio
Example usage
我在 Apple 自己的示例中找到了一个 UIImage 类别,它具有相同的技巧。这是链接: https://developer.apple.com/library /ios/samplecode/sc2273/Listings/AirDropSample_UIImage_Resize_m.html。
调用更改
您只需将
imageWithImage:scaledToSize:inRect:
中的为:即可考虑图像中的 Alpha 通道。
I found a category for UIImage in Apple's own examples which does the same trick. Here's the link: https://developer.apple.com/library/ios/samplecode/sc2273/Listings/AirDropSample_UIImage_Resize_m.html.
You'll just have to change the call:
in
imageWithImage:scaledToSize:inRect:
with:In order to consider the alpha channel in the image.
对于我的 Xamarin 同胞,这里是 @Paul Lynch 答案的 Xamarin.iOS C# 版本。
For my fellow Xamarians, here is a Xamarin.iOS C# version of @Paul Lynch answer.
无需拉伸图像的有效方法Swift 4
//调用方法
Effective approach without stretching image Swift 4
// Call method
如果您想制作 UIImage 的缩略图(按比例调整大小或可能涉及一些裁剪),请查看 UIImage+ Resize 类别允许您使用简洁的、类似 ImageMagick 的语法:
If you want to make a thumbnail of a UIImage (with proportional resizing or maybe some cropping involved), check out UIImage+Resize category that allows you to use concise, ImageMagick-like syntax:
[cf Chris] 将大小调整为所需的大小:
或者等效地替换
CGImageGetWidth(...)/DESIREDWIDTH
[cf Chris] To resize to a desired size:
or, equivalently, substitute
CGImageGetWidth(...)/DESIREDWIDTH
Rogerio Chaves 答案作为快速扩展
还有奖金
Rogerio Chaves answer as a swift extension
And also bonus
带有故障保护选项的 Swift 3.0(出现错误时返回原始图像):
Swift 3.0 with failsafe option (returns the original image in case of error):
(兼容 Swift 4)iOS 10+ 和 iOS < 10 解决方案(如果可能,使用 UIGraphicsImageRenderer,否则使用 UIGraphicsGetImageFromCurrentImageContext)
(Swift 4 compatible) iOS 10+ and iOS < 10 solution (using
UIGraphicsImageRenderer
if possible,UIGraphicsGetImageFromCurrentImageContext
otherwise)@Paul Lynch 的答案很好,但它会改变图像比例。
如果您不想更改图像比例,但仍希望新图像适合新尺寸,请尝试此操作。
@Paul Lynch's answer is great, but it would change the image ratio.
if you don`t want to change the image ratio, and still want the new image fit for new size, try this.
使用这个扩展
use this extension
有时您的图像的
scale
大于1
,因此调整图像大小会使图像出现意外的情况。这是我针对这个案例的解决方案。Some time your image have
scale
large than1
so that resize image will make an image unexpected. This is my solution for this case.