在UIImage周围添加透明空间

比方说,我们有一个600X400的图像,我们想要一个1000X100的新图像,其中包含中心的初始图像和周围的透明空间。 我怎样才能在代码中实现?

在这里输入图像说明

您创build一个新的图像上下文是1000×1000,在中间绘制旧图像,然后从上下文中获取新的UIImage

 // Setup a new context with the correct size CGFloat width = 1000; CGFloat height = 1000; UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(context); // Now we can draw anything we want into this new context. CGPoint origin = CGPointMake((width - oldImage.size.width) / 2.0f, (height - oldImage.size.height) / 2.0f); [oldImage drawAtPoint:origin]; // Clean up and get the new image. UIGraphicsPopContext(); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

在Swift中,你可以写一个UIImage的扩展,在它的周围用insets绘制图像。

Swift 3

 import UIKit extension UIImage { func imageWithInsets(insets: UIEdgeInsets) -> UIImage? { UIGraphicsBeginImageContextWithOptions( CGSize(width: self.size.width + insets.left + insets.right, height: self.size.height + insets.top + insets.bottom), false, self.scale) let _ = UIGraphicsGetCurrentContext() let origin = CGPoint(x: insets.left, y: insets.top) self.draw(at: origin) let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return imageWithInsets } } 

老解答

 import UIKit extension UIImage { func imageWithInsets(insets: UIEdgeInsets) -> UIImage { UIGraphicsBeginImageContextWithOptions( CGSizeMake(self.size.width + insets.left + insets.right, self.size.height + insets.top + insets.bottom), false, self.scale) let context = UIGraphicsGetCurrentContext() let origin = CGPoint(x: insets.left, y: insets.top) self.drawAtPoint(origin) let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return imageWithInsets } } 

这是Swift 3在DrummerB的灵感答案:

 import UIKit extension UIImage { func addImagePadding(x: CGFloat, y: CGFloat) -> UIImage? { let width: CGFloat = self.size.width + x let height: CGFloat = self.size.height + y UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0) let origin: CGPoint = CGPoint(x: (width - self.size.width) / 2, y: (height - self.size.height) / 2) self.draw(at: origin) let imageWithPadding = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return imageWithPadding } } 

如何申请:

 let image = UIImage(named: "your-image")! let imageView = UIImageView(image: image.addImagePadding(x: 50, y: 50)) imageView.backgroundColor = UIColor.gray view.addSubview(imageView) 

特征:

  • 只需通过parameter passing填充值
  • 彩色填充(通过将UIGraphicsBeginImageContextWithOptions opaque参数设置为false)

在UIImage上做一个类别,试试这个:

 + (UIImage *)imageWithInsets:(CGRect)insetRect image:(UIImage *)image { CGRect newRect = CGRectMake(0.0, 0.0, insetRect.origin.x+insetRect.size.width+image.size.width, insetRect.origin.y+insetRect.size.height+image.size.height); // Setup a new context with the correct size UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(context); // Now we can draw anything we want into this new context. CGPoint origin = CGPointMake(insetRect.origin.x, insetRect.origin.y); [image drawAtPoint:origin]; // Clean up and get the new image. UIGraphicsPopContext(); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }