Draw another image on a UIImage

后端 未结 2 1804
情深已故
情深已故 2020-11-30 06:47

Is it possible to add another, smaller, image to a UIImage/UIImageView? If so, how? If not, then how can I draw a small filled triangle?

Thanks

2条回答
  •  温柔的废话
    2020-11-30 07:27

    You can try this, works perfect for me, it's UIImage category:

    - (UIImage *)drawImage:(UIImage *)inputImage inRect:(CGRect)frame {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
        [self drawInRect:CGRectMake(0.0, 0.0, self.size.width, self.size.height)];
        [inputImage drawInRect:frame];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    or Swift:

    extension UIImage {
        func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! {
            UIGraphicsBeginImageContext(size)
            draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
            image.draw(in: rect)
            let result = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return result
        }
    }
    

提交回复
热议问题