我有一个 UIImage
,我想更改其特定像素的图像,例如 100 x 100 像素。我怎样才能做到这一点?
最佳答案
您可以在该像素上添加一个矩形并用颜色填充它:
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)
}
或者您可能想将 1 x 1 像素的 UIImage 嵌入到其他 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))
}
或者您可能想将 UIImage 的像素移动到另一个 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))
}
关于ios - 如何更改 UIImageView 中单个像素的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443633/