How can I make an UIImage programmatically?

前端 未结 5 1795
自闭症患者
自闭症患者 2021-02-07 02:26

This isn\'t what you probably thought it was to begin with. I know how to use UIImage\'s, but I now need to know how to create a \"blank\" UIImage using:

CGRect sc

相关标签:
5条回答
  • 2021-02-07 02:30

    Using the latest UIGraphics classes, and in swift, this looks like this (note the CGContextDrawPath that is missing in the answer from user1834305, this is the reason that is produces an transparant image) :

    static func imageWithSize(size : CGSize, color : UIColor) -> UIImage {
        UIGraphicsBeginImageContext(size)
        let context = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextAddRect(context, CGRect(x: 0, y: 0, width: size.width, height: size.height));
        CGContextDrawPath(context, .Fill)
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image
    }
    
    0 讨论(0)
  • 2021-02-07 02:35

    Based on @HixField and @Rudolf Adamkovič answer. here's an extension which returns an optional, which I believe is the correct way to do this (correct me if I'm wrong!)?

    This extension allows you to create a an empty UIImage of what ever size you need (up to memory limit) with what ever fill color you want, which defaults to white, if you want the image to be the clear color you would use something like the following:

    let size = CGSize(width: 32.0, height: 32.0)
    if var image = UIImage.imageWithSize(size:size, UIColor.clear) {
        //image was successfully created, do additional stuff with it here.
    }
    

    This is for swift 3.x:

    extension UIImage {
         static func imageWithSize(size : CGSize, color : UIColor = UIColor.white) -> UIImage? {
             var image:UIImage? = nil
             UIGraphicsBeginImageContext(size)
             if let context = UIGraphicsGetCurrentContext() {
                   context.setFillColor(color.cgColor)
                   context.addRect(CGRect(origin: CGPoint.zero, size: size));
                   context.drawPath(using: .fill)
                   image = UIGraphicsGetImageFromCurrentImageContext();
            }
            UIGraphicsEndImageContext()
            return image
        }
    }
    
    0 讨论(0)
  • 2021-02-07 02:42

    You need to use CoreGraphics, as follows.

    CGSize size = CGSizeMake(desiredWidth, desiredHeight);
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    [[UIColor whiteColor] setFill];
    UIRectFill(CGRectMake(0, 0, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    The code creates a new CoreGraphics image context with the options passed as parameters; size, opaqueness, and scale. By passing 0 for scale, iOS automatically chooses the appropriate value for the current device.

    Then, the context fill colour is set to [UIColor whiteColor]. Immediately, the canvas is then actually filled with that color, by using UIRectFill() and passing a rectangle which fills the canvas.

    A UIImage is then created of the current context, and the context is closed. Therefore, the image variable contains a UIImage of the desired size, filled white.

    0 讨论(0)
  • 2021-02-07 02:46

    Swift version:

    extension UIImage {
    
        static func emptyImage(with size: CGSize) -> UIImage? {
            UIGraphicsBeginImageContext(size)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    
    }
    
    0 讨论(0)
  • 2021-02-07 02:47

    If you want to draw just an empty image, you could use UIKit UIImageBeginImageContextWithOptions: method.

        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height)); // this may not be necessary
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    

    The code above assumes that you draw a image with a size of width x height. It adds rectangle into the graphics context, but it may not be necessary. Try it yourself. This is the way to go. :)

    Or, if you want to create a snapshot of your current view you would type code like;

     UIGraphicsBeginImageContext(CGSizeMake(self.view.size.width, self.view.size.height));
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    

    Don't forget to include Quartz library if you use layer.

    0 讨论(0)
提交回复
热议问题