加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift 3中使用CGPoint从图像中获取像素颜色

发布时间:2020-12-14 04:28:48 所属栏目:百科 来源:网络整理
导读:我在 Swift 3中尝试这个PixelExtractor类,得到一个错误; 无法使用类型'(UnsafeMutableRawPointer?)’的参数列表调用类型’UnsafePointer’的初始值设定项 class PixelExtractor: NSObject {let image: CGImagelet context: CGContextRef?var width: Int { g
我在 Swift 3中尝试这个PixelExtractor类,得到一个错误;
无法使用类型'(UnsafeMutableRawPointer?)’的参数列表调用类型’UnsafePointer’的初始值设定项

class PixelExtractor: NSObject {

let image: CGImage
let context: CGContextRef?

var width: Int {
    get {
        return CGImageGetWidth(image)
    }
}

var height: Int {
    get {
        return CGImageGetHeight(image)
    }
}

init(img: CGImage) {
    image = img
    context = PixelExtractor.createBitmapContext(img)
}

class func createBitmapContext(img: CGImage) -> CGContextRef {

    // Get image width,height
    let pixelsWide = CGImageGetWidth(img)
    let pixelsHigh = CGImageGetHeight(img)

    let bitmapBytesPerRow = pixelsWide * 4
    let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)

    // Use the generic RGB color space.
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    let bitmapData = malloc(bitmapByteCount)
    let bitmapInfo = CGBitmapInfo(rawValue:   CGImageAlphaInfo.PremultipliedFirst.rawValue)
    let size = CGSizeMake(CGFloat(pixelsWide),CGFloat(pixelsHigh))
    UIGraphicsBeginImageContextWithOptions(size,false,0.0)
    // create bitmap
    let context = CGBitmapContextCreate(bitmapData,pixelsWide,pixelsHigh,8,bitmapBytesPerRow,colorSpace,bitmapInfo.rawValue)

    // draw the image onto the context
    let rect = CGRect(x: 0,y: 0,width: pixelsWide,height: pixelsHigh)
    CGContextDrawImage(context,rect,img)

    return context!
}

func colorAt(x x: Int,y: Int)->UIColor {

    assert(0<=x && x<width)
    assert(0<=y && y<height)

    let uncastedData = CGBitmapContextGetData(context)
    let data = UnsafePointer<UInt8>(uncastedData)

    let offset = 4 * (y * width + x)

    let alpha: UInt8 = data[offset]
    let red: UInt8 = data[offset+1]
    let green: UInt8 = data[offset+2]
    let blue: UInt8 = data[offset+3]

    let color = UIColor(red: CGFloat(red)/255.0,green: CGFloat(green)/255.0,blue: CGFloat(blue)/255.0,alpha: CGFloat(alpha)/255.0)

    return color
}

}

修复此错误.

let data = UnsafePointer<UInt8>(uncastedData)

– >

let data = UnsafeRawPointer(uncastedData)

得到其他错误; ‘输入’UnsafeRawPointer?’没有下标成员’

如何修改此错误?

解决方法

当您的数据中包含UnsafeRawPointer时,您可以编写类似的内容:

let alpha = data.load(fromByteOffset: offset,as: UInt8.self)
    let red = data.load(fromByteOffset: offset+1,as: UInt8.self)
    let green = data.load(fromByteOffset: offset+2,as: UInt8.self)
    let blue = data.load(fromByteOffset: offset+3,as: UInt8.self)

或者,您可以获取UnsafeMutablePointer< UInt8>来自你的uncastedData(假设它是一个UnsafeMutableRawPointer):

let data = uncastedData.assumingMemoryBound(to: UInt8.self)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读