-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUIImage+PixelData.swift
More file actions
36 lines (33 loc) · 1.2 KB
/
UIImage+PixelData.swift
File metadata and controls
36 lines (33 loc) · 1.2 KB
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
//
// UIImage+PixelData.swift
// Project SF
//
// Created by Roman Esin on 16.07.2020.
//
import UIKit
extension UIImage {
convenience init?(pixelImage: PixelImage) {
let width = pixelImage.width
let height = pixelImage.height
let pixels = pixelImage.pixels
guard width > 0 && height > 0, pixels.count == width * height else { return nil }
var data = pixels
guard let providerRef = CGDataProvider(data: Data(bytes: &data,
count: data.count * MemoryLayout<Pixel>.size) as CFData)
else { return nil }
guard let cgim = CGImage(
width: width,
height: height,
bitsPerComponent: 8,
bitsPerPixel: 32,
bytesPerRow: width * MemoryLayout<Pixel>.size,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue),
provider: providerRef,
decode: nil,
shouldInterpolate: false,
intent: .defaultIntent)
else { return nil }
self.init(cgImage: cgim)
}
}