简体   繁体   English

Swift-选择UIImage的像素颜色-内存崩溃

[英]Swift - Picking pixel colour of UIImage - memory crash

I want to pick colour of specific pixel of UIImage in Swift 3 and this method is called ~10k times. 我想在Swift 3中选择UIImage特定像素的颜色,此方法称为〜10k次。

 func pixelColour(_ pixelPosition: CGPoint) {

    if !CGRect(x: 0.0, y: 0.0, width: self.size.width, height: self.size.height).contains(pixelPosition) {
        return false
    }


    let pointX = trunc(pixelPosition.x);
    let pointY = trunc(pixelPosition.y);

    let cgImage = self.cgImage;
    let width = self.size.width;
    let height = self.size.height;

    let colorSpace = CGColorSpaceCreateDeviceRGB();
    let bytesPerPixel = 4;
    let bytesPerRow = bytesPerPixel * 1;
    let bitsPerComponent = 8;
    let pixelData =  UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: 4)
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

    let context = CGContext(data: pixelData, width: 1, height: 1, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

    context?.setBlendMode(CGBlendMode.copy);
    context?.translateBy(x: -pointX, y: pointY-CGFloat(height));

    // This line takes too much memory, how to release memory here?
    context?.draw(cgImage!, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(width), height: CGFloat(height)));




 print("\(pixelData[0]) \(pixelData[1])  \(pixelData[2]) ")
    pixelData.deallocate(capacity: 4)


}

Unfortunately it seems that memory is not released, because it crashes after checking ~500 pixels. 不幸的是,似乎没有释放内存,因为它在检查约500个像素后崩溃。 How I can solve this problem? 我该如何解决这个问题?

You have not shown how pixelColour is called, but I presume that it is in some sort of loop. 您尚未显示如何pixelColour ,但我认为它处于某种循环中。 If so, surround your repeated call to pixelColour with an autoreleasepool{...} call to release the accumulated memory each time through your loop: 如果是这样,请在pixelColour中用autoreleasepool{...}调用围绕对pixelColour的重复调用,以在每次循环中释放累积的内存:

let p = // next CGPoint
autoreleasepool {
    pixelColour(p)
}

Taken from How do I get the color of a pixel in a UIImage with Swift? 取自如何在Swift中获取UIImage中像素的颜色?

and converted to Swift 3 并转换为Swift 3

extension UIImage{
    func getPixelColor(pos: CGPoint) -> UIColor? {
        guard let provider=cgImage?.dataProvider else{
            return nil
        }
        let pixelData = provider.data
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

        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 UIColor(red: r, green: g, blue: b, alpha: a)
    }
}

This should not take to much memory, since it uses pointers so it doesn't load the entire image. 这应该不会占用太多内存,因为它使用了指针,因此不会加载整个图像。

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM