[英]How to change colour of individual pixel of UIImage/UIImageView
[英]How to change the image of an individual pixel in UIImageView
我有一個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)
}
或者也許你想在其他UIImage中嵌入一個1 x 1像素的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))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.