问题1:我有从图像中获取的RGB值,并且想要将此RGB值重新转换为UIImage。有人帮助我,请坚持。我在这里发布我的代码。 问题2:还建议我写有关RGB到HSV,HSV到RGB的任何文章。最基本的问题是hmmm,为什么我们要获取RGB,HSV,修改RGB,HSV值并再次转换为图像。
代码部分:它是我的代码,我还显示了我的代码的输出。 我正在使用Xcode 9.4.1 语言是Swift 4.1 [其输出] [1]:https://i.stack.imgur.com/2BTSP.png [将其转换为rgb的图片] [1]:https://i.stack.imgur.com/GEoii.png
`import UIKit
public struct PixelData {
var a: CGFloat = 0
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
}
class ViewController: UIViewController{
@IBOutlet weak var myImagView: UIImageView!
var myConvertImage : UIImage!
var pixelArray : [PixelData] = []
override func viewDidLoad() {
super.viewDidLoad()
var color : RGBColorSpace!
var pixelDataObj = PixelData()
let image : UIImage = UIImage(named:"img50")!
let hight = image.cgImage?.height
let width = image.cgImage?.width
for xx in 1...hight! {
for yy in 1...width! {
let point = CGPoint(x: xx, y: yy)
color = image.getPixelColor(pos: point)
pixelDataObj.r = color.red
pixelDataObj.g = color.green
pixelDataObj.b = color.blue
pixelDataObj.a = color.alpha
pixelArray.append(pixelDataObj)
}
}
print("pixelArray.count is \(pixelArray.count)")
myConvertImage = imageFromARGB32Bitmap(pixels: pixelArray, width: width!, height: hight!)
if myConvertImage != nil {
let img = myConvertImage!
myImagView.image = img
}else {print("no image")}
}
func imageFromARGB32Bitmap(pixels: [PixelData], width: Int, height: Int) -> UIImage? {
guard width > 0 && height > 0 else { return nil }
print("width*height is = \(width*height)")
// guard pixels.count == width * height else { return nil }
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
let bitsPerComponent = 8
let bitsPerPixel = 32
var data = pixels // Copy to mutable []
guard let providerRef = CGDataProvider(data: NSData(bytes: &data,
length: data.count * MemoryLayout<PixelData>.size)
)
else { return nil }
guard let cgim = CGImage(
width: width,
height: height,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: width * MemoryLayout<PixelData>.size,
space: rgbColorSpace,
bitmapInfo: bitmapInfo,
provider: providerRef,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent
[enter image description here][1] )
else { return nil }
return UIImage(cgImage: cgim)
}
}
extension UIImage {
func getPixelColor(pos: CGPoint) -> RGBColorSpace {
guard let cgImage = cgImage, let pixelData = cgImage.dataProvider?.data else { return RGBColorSpace(red: 0,green: 0,blue: 0,alpha: 0)}
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
let bytesPerPixel = cgImage.bitsPerPixel / 8
// adjust the pixels to constrain to be within the width/height of the image
let y = pos.y > 0 ? pos.y - 1 : 0
let x = pos.x > 0 ? pos.x - 1 : 0
let pixelInfo = ((Int(self.size.width) * Int(y)) + Int(x)) * bytesPerPixel
let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)
return RGBColorSpace(red: r, green: g, blue: b, alpha: a)
}
}
struct RGBColorSpace {
var red : CGFloat
var green : CGFloat
var blue : CGFloat
var alpha : CGFloat
}`