HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何逐个像素地创建图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在 swift 3中逐像素地创建一个UI Image

搜索过但找不到实际有效的代码

所以让我解释一下,我有一个带字符的数组

var array = ["w","x","y","w","y"] //there will be like 26 millions of those

如果是w,则像素的颜色将为蓝色

如果它是x,则像素的颜色将为红色

如果是y,则像素的颜色为绿色

如果是v,则像素的颜色为黑色

我想从这些角色创建一个图像并将其存储在照片中

有什么想法吗??

谢谢你的回答

解决方法

可以创建一个CGContext,然后检索该图像的数据缓冲区,然后使用与您的字符串值对应的值填充该缓冲区:

func createImage(width: Int,height: Int,from array: [String],completionHandler: @escaping (UIImage?,String?) -> Void) {
    DispatchQueue.global(qos: .utility).async {
        let colorSpace       = CGColorSpaceCreateDeviceRGB()
        let bytesPerPixel    = 4
        let bitsPerComponent = 8
        let bytesPerRow      = bytesPerPixel * width
        let bitmapInfo       = RGBA32.bitmapInfo

        guard array.count == width * height else {
            completionHandler(nil,"Array size \(array.count) is incorrect given dimensions \(width) x \(height)")
            return
        }

        guard let context = CGContext(data: nil,width: width,height: height,bitsPerComponent: bitsPerComponent,bytesPerRow: bytesPerRow,space: colorSpace,bitmapInfo: bitmapInfo) else {
            completionHandler(nil,"unable to create context")
            return
        }

        guard let buffer = context.data else {
            completionHandler(nil,"unable to get context data")
            return
        }

        let pixelBuffer = buffer.bindMemory(to: RGBA32.self,capacity: width * height)

        for (index,String) in array.enumerated() {
            switch String {
            case "w": pixelBuffer[index] = .blue
            case "x": pixelBuffer[index] = .red
            case "y": pixelBuffer[index] = .green
            case "v": pixelBuffer[index] = .black
            default: completionHandler(nil,"Unexpected value: \(String)"); return
            }
        }

        let cgImage = context.makeImage()!

        let image = UIImage(cgImage: cgImagE)

        // or
        //
        // let image = UIImage(cgImage: cgImage,scale: UIScreen.main.scale,orientation: .up)

        completionHandler(image,nil)
    }

}

如果有2600万像素,您可能希望使此异步以避免阻塞主队列.

便说一句,上面使用这个结构:

struct RGBA32: Equatable {
    private var color: UInt32

    var redComponent: UInt8 {
        return UInt8((color >> 24) & 255)
    }

    var greenComponent: UInt8 {
        return UInt8((color >> 16) & 255)
    }

    var blueComponent: UInt8 {
        return UInt8((color >> 8) & 255)
    }

    var alphaComponent: UInt8 {
        return UInt8((color >> 0) & 255)
    }

    init(red: UInt8,green: UInt8,blue: UInt8,alpha: UInt8) {
        color = (UInt32(red) << 24) | (UInt32(green) << 16) | (UInt32(bluE) << 8) | (UInt32(alpha) << 0)
    }

    static let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Little.rawValue

    static func ==(lhs: RGBA32,rhs: RGBA32) -> Bool {
        return lhs.color == rhs.color
    }

    static let black = RGBA32(red: 0,green: 0,blue: 0,alpha: 255)
    static let red   = RGBA32(red: 255,alpha: 255)
    static let green = RGBA32(red: 0,green: 255,alpha: 255)
    static let blue  = RGBA32(red: 0,blue: 255,alpha: 255)
}

要保存图像,您可以执行以下操作:

createImage(width: width,from: array) { image,errormessage in
    guard let image = image,errormessage == nil else {
        print(errormessage!)
        return
    }

    DispatchQueue.main.async {
        self.imageView.image = image
        UIImageWriteToSavedPhotosAlbum(image,self,#SELEctor(self.image(_:didFinishSavingWithError:contexTinfo:)),nil)
    }
}

哪里

func image(_ image: UIImage,didFinishSavingWithError error: Error?,contexTinfo: Any?) {
    guard error == nil else {
        print(error!.localizedDescription)
        return
    }

    print("image saved")
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何逐个像素地创建图像全部内容,希望文章能够帮你解决ios – 如何逐个像素地创建图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。