-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageExtensions.swift
More file actions
executable file
·50 lines (45 loc) · 1.4 KB
/
ImageExtensions.swift
File metadata and controls
executable file
·50 lines (45 loc) · 1.4 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// ALImageExtensions.swift
// ALCameraViewController
//
// Created by Alex Littlejohn on 2015/11/06.
// Copyright © 2015 zero. All rights reserved.
//
import UIKit
extension UIImage {
func crop(frame: CGRect, scale: CGFloat) -> UIImage {
if frame.size == size && frame.origin == CGPoint.zero {
return self
}
let drawPoint = CGPointZero
UIGraphicsBeginImageContext(frame.size)
let context = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(context, -frame.origin.x, -frame.origin.y)
drawAtPoint(drawPoint)
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return croppedImage
}
func fixFrontCameraOrientation() -> UIImage {
var newOrient:UIImageOrientation
switch imageOrientation {
case .Up:
newOrient = .UpMirrored
case .UpMirrored:
newOrient = .Up
case .Down:
newOrient = .DownMirrored
case .DownMirrored:
newOrient = .Down
case .Left:
newOrient = .RightMirrored
case .LeftMirrored:
newOrient = .Right
case .Right:
newOrient = .LeftMirrored
case .RightMirrored:
newOrient = .Left
}
return UIImage(CGImage: CGImage!, scale: scale, orientation: newOrient)
}
}