-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageHelper.swift
71 lines (60 loc) · 2.14 KB
/
ImageHelper.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
//
// ImageHelper.swift
//
// Created by Sven Bautz on 18.06.15.
// Copyright (c) Sven Bautz
//
import Foundation
class ImageHelper {
/**
returns UIImage with expected height
**/
class func resizeImage(image: UIImage, newHeight: CGFloat) -> UIImage {
let scale = newHeight / image.size.height
let newWidth = image.size.width * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
/**
returns UImage with expected width
**/
class func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
class func resizeImage(image: UIImage, longestSide: CGFloat) -> UIImage {
if image.size.height > image.size.width {
return resizeImage(image, newHeight: longestSide)
} else {
return resizeImage(image, newWidth: longestSide)
}
}
/**
returns an base64 encoded string from UIImage
**/
class func base64FromImage(image: UIImage, quality: CGFloat) -> String {
var imageBase64 = ""
if let imageData = UIImageJPEGRepresentation(image,quality){
imageBase64 = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
}
return imageBase64
}
/**
returns UIImage from a base64 encoded string
**/
class func imageFromBase64(base64: String) -> UIImage? {
var image : UIImage? = nil
if let imageData = NSData(base64EncodedString: base64, options:NSDataBase64DecodingOptions.allZeros) {
image = UIImage(data: imageData)
}
return image
}
}