-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelExtractor.swift
79 lines (60 loc) · 2.5 KB
/
PixelExtractor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// PixelExtractor.swift
// ColorMatcher
//
// Created by vdab cursist on 24/10/2017.
// Copyright © 2017 vdab cursist. All rights reserved.
//
import Foundation
import CoreGraphics
import UIKit
class PixelExtractor: NSObject {
let image: CGImage
let context: CGContext?
var width:Int {
get {
return image.width
}
}
var height:Int {
get {
return image.height
}
}
init(img: CGImage) {
image = img
context = PixelExtractor.createBitmapContext(img: img)
}
class func createBitmapContext(img: CGImage) -> CGContext {
let pixelsWide = img.width
let pixelsHigh = img.height
let bitmapBytesPerRow = pixelsWide * 4
let bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapData = malloc(bitmapByteCount)
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
let size = CGSize(width: pixelsWide, height: pixelsHigh)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let context = CGContext(data: bitmapData, width: pixelsWide, height: pixelsHigh, bitsPerComponent: 8,
bytesPerRow: bitmapBytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
let rect = CGRect(x: 0, y: 0, width: pixelsWide, height: pixelsHigh)
context?.draw(img, in: rect)
return context!
}
//
func getPixelColorAtPoint(point: CGPoint, sourceView: UIView) -> UIColor {
let pixel = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: 4)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(data: pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
context!.translateBy(x: -point.x, y: -point.y)
sourceView.layer.render(in: context!)
let color: UIColor = UIColor(red: CGFloat(pixel[0])/255.0,
green: CGFloat(pixel[1])/255.0,
blue: CGFloat(pixel[2])/255.0,
alpha: CGFloat(pixel[3])/255.0)
print(color)
pixel.deallocate(capacity: 4)
return color
}
}