How to get pixel color at location from UIimage scaled within a UIimageView

前端 未结 3 1515
悲哀的现实
悲哀的现实 2020-12-20 01:23

I\'m currently using this technique to get the color of a pixel in a UIimage. (on Ios)

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
UIColor* color =         


        
3条回答
  •  囚心锁ツ
    2020-12-20 01:43

    try this for swift3

    func getPixelColor(image: UIImage, x: Int, y: Int, width: CGFloat) -> UIColor 
    {
    
        let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
        let data: UnsafePointer = CFDataGetBytePtr(pixelData)
    
        let pixelInfo: Int = ((Int(width) * y) + 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)
    }
    

提交回复
热议问题