在UIImage周围添加透明空间

Tas*_*ris 28 cocoa-touch uiimage ios

假设我们有600X400的图像,我们希望最终得到1000X100的新图像,其中包含中心的初始图像和周围的透明空间.我怎样才能在代码中实现这一点?

在此输入图像描述

mix*_*xel 36

在Swift中,你可以为UIImage编写一个扩展,用它绘制带有插图的图像.

斯威夫特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
    }
}
Run Code Online (Sandbox Code Playgroud)

老答案:

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
    }
}
Run Code Online (Sandbox Code Playgroud)


Dru*_*erB 23

您创建一个1000x1000的新图像上下文,在中间绘制旧图像,然后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();
Run Code Online (Sandbox Code Playgroud)


app*_*ted 20

这是Swift 4的解决方案,灵感来自DrummerB的回答:

import UIKit

extension UIImage {

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

        return imageWithPadding
    }
}
Run Code Online (Sandbox Code Playgroud)

如何申请:

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

特征:

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