-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImageExtension.swift
92 lines (70 loc) · 3.29 KB
/
UIImageExtension.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
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// UIImageExtension.swift
// Blink
//
// Created by Albert Podusenko on 22.03.16.
// Copyright © 2016 Albert Podusenko. All rights reserved.
//
import Foundation
extension UIImage {
public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
let degreesToRadians: (CGFloat) -> CGFloat = {
return $0 / 180.0 * CGFloat(M_PI)
}
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);
// // Rotate the image context
CGContextRotateCTM(bitmap, degreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat
if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}
CGContextScaleCTM(bitmap, yFlip, -1.0)
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
public func cropsToSquare() -> UIImage {
let refWidth = CGFloat(CGImageGetWidth(self.CGImage))
let refHeight = CGFloat(CGImageGetHeight(self.CGImage))
let cropSize = refWidth > refHeight ? refHeight : refWidth
let x = (refWidth - cropSize) / 2.0
let y = (refHeight - cropSize) / 2.0
let cropRect = CGRectMake(x, y, cropSize, cropSize)
let imageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect)
let cropped = UIImage(CGImage: imageRef!, scale: 0.0, orientation: self.imageOrientation)
return cropped
}
}
/*
http://stackoverflow.com/questions/14383932/convert-cmsamplebufferref-to-uiimage
*/
func imageFromSampleBuffer(pixelBuffer: CVImageBuffer) -> UIImage? {
let colorSpace = CGColorSpaceCreateDeviceRGB()
CVPixelBufferLockBaseAddress(pixelBuffer, 0)
let address = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
let context = CGBitmapContextCreate(address, width, height, 8,bytesPerRow, colorSpace, bitmapInfo.rawValue);
let imageRef = CGBitmapContextCreateImage(context)
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
var resultImage : UIImage?
if context != nil {
resultImage = UIImage(CGImage: imageRef!)
}
return resultImage
}