Created
September 19, 2018 15:57
-
-
Save liufsd/a3934b2dbf2129a86002ec5a93c45f8c to your computer and use it in GitHub Desktop.
UIImage Pixel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
func maxArea() -> CGRect{ | |
let provider = self.cgImage!.dataProvider | |
let providerData = provider!.data | |
let data = CFDataGetBytePtr(providerData) | |
var minX = 0 | |
var minY = 0 | |
var maxX = 0 | |
var maxY = 0 | |
for row in 0..<Int(self.size.height) { | |
for col in 0..<Int(self.size.width) { | |
let numberOfComponents = 4 | |
let pixelData = ((Int(size.width) * row) + col) * numberOfComponents | |
let r = CGFloat(data![pixelData]) / 255.0 | |
let g = CGFloat(data![pixelData + 1]) / 255.0 | |
let b = CGFloat(data![pixelData + 2]) / 255.0 | |
let a = CGFloat(data![pixelData + 3]) / 255.0 | |
if r != 0 || g != 0 || b != 0 || a != 0 { | |
if r == 1 && g == 1 && b == 1 && a == 1 { | |
continue | |
} else { | |
if row >= maxY { | |
maxY = row | |
} | |
if row <= minY { | |
minY = row | |
} | |
if col >= maxX { | |
maxX = col | |
} | |
if col <= minX { | |
minX = col | |
} | |
} | |
} | |
} | |
} | |
return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY) | |
} | |
func pixelData() -> [Pixel] { | |
let provider = self.cgImage!.dataProvider | |
let providerData = provider!.data | |
let data = CFDataGetBytePtr(providerData) | |
var r, g, b, a: UInt8 | |
var pixels: [Pixel] = [] | |
for row in 0..<Int(self.size.height) { | |
for col in 0..<Int(self.size.width) { | |
// r = data.memory | |
// data = data?.advancedBy(1) | |
// g = data.memory | |
// data = data.advancedBy(1) | |
// b = data.memory | |
// data = data.advancedBy(1) | |
// a = data.memory | |
// data = data.advancedBy(1) | |
// pixels.append(Pixel(r: r, g: g, b: b, a: a, row: row, col: col)) | |
let numberOfComponents = 4 | |
let pixelData = ((Int(size.width) * col) + row) * numberOfComponents | |
let r = CGFloat(data![pixelData]) / 255.0 | |
let g = CGFloat(data![pixelData + 1]) / 255.0 | |
let b = CGFloat(data![pixelData + 2]) / 255.0 | |
let a = CGFloat(data![pixelData + 3]) / 255.0 | |
pixels.append(Pixel(r: UInt8(r), g: UInt8(g), b: UInt8(b), a: UInt8(a), row: row, col: col)) | |
} | |
} | |
return pixels | |
} | |
} | |
struct Pixel { | |
var r: Float | |
var g: Float | |
var b: Float | |
var a: Float | |
var row: Int | |
var col: Int | |
init(r: UInt8, g: UInt8, b: UInt8, a: UInt8, row: Int, col: Int) { | |
self.r = Float(r) | |
self.g = Float(g) | |
self.b = Float(b) | |
self.a = Float(a) | |
self.row = row | |
self.col = col | |
} | |
var color: UIColor { | |
return UIColor(red: CGFloat(r/255.0), green: CGFloat(g/255.0), blue: CGFloat(b/255.0), alpha: CGFloat(a/255.0)) | |
} | |
var description: String { | |
return "RGBA(\(r), \(g), \(b), \(a))" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment