简体   繁体   中英

How to change the image of an individual pixel in UIImageView

I have a UIImage , I want to change image of its particular pixel, say 100 x 100 pixel. How can I do this?

You can add a rectangle over that pixel and fill it with a color:

var image: UIImage!
let color = UIColor.red
image = UIGraphicsImageRenderer(size: image.size).image { context in
    image.draw(at: .zero)
    let rectangle = CGRect(x: 100, y: 100, width: 1, height: 1)
    color.setFill()
    context.fill(rectangle)
}

Or maybe yo want to embed a 1 x 1 pixel UIImage into other UIImage:

var onePixel: UIImage!
var image: UIImage!
image = UIGraphicsImageRenderer(size: image.size).image { context in
    image.draw(at: .zero)
    onePixel.draw(at: CGPoint(x: 100, y: 100))
}

Or maybe you want to move a pixel of an UIImage to another UIImage:

var image1: UIImage!
var image2: UIImage!
// 1 x 1 size image, from position 100 x 100 of image2
var pixel: UIImage = UIGraphicsImageRenderer(bounds: CGRect(x: 100, y: 100, width: 1, height: 1)).image(actions: { _ in
    image2.draw(at: .zero)
})
// The image1 with the pixel image embed in the position 100 x 100
var image1WithPixel = UIGraphicsImageRenderer(size: image1.size).image { (_) in
    image1.draw(at: .zero)
    pixel.draw(at: CGPoint(x: 100, y: 100))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM