ios - 如何更改 UIImageView 中单个像素的图像

标签 ios iphone uiimageview pixel

我有一个 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/

相关文章:

ios - 如果设备支持 Touch ID

ios - CloudKit 可以创建共享私有(private)容器吗?

iphone - 语义问题和解析问题 : Missing context for method declaration & expected method body

javascript - 如何在iPhone上使用windowScriptObject?

ios - 屏幕旋转后 UIImageView 和 UIButton 图像困惑

iphone - iOS SDK 中的 Facebook 照片大小

ios - 单击按钮更改背景图像

ios - UIButton - 触摸更改图像

ios - 显示和隐藏键盘时 UITableView 部分页脚出现问题

iphone - 如何使用蓝牙测量两个iPhone设备之间的近距离?