Compression.m
5.3 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Compression.m
// imageCropPicker
//
// Created by Ivan Pusic on 12/24/16.
// Copyright © 2016 Ivan Pusic. All rights reserved.
//
#import "Compression.h"
@implementation Compression
- (instancetype)init {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:@{
@"640x480": AVAssetExportPreset640x480,
@"960x540": AVAssetExportPreset960x540,
@"1280x720": AVAssetExportPreset1280x720,
@"1920x1080": AVAssetExportPreset1920x1080,
@"LowQuality": AVAssetExportPresetLowQuality,
@"MediumQuality": AVAssetExportPresetMediumQuality,
@"HighestQuality": AVAssetExportPresetHighestQuality,
@"Passthrough": AVAssetExportPresetPassthrough,
}];
if (@available(iOS 9.0, *)) {
[dic addEntriesFromDictionary:@{@"3840x2160": AVAssetExportPreset3840x2160}];
} else {
// Fallback on earlier versions
}
self.exportPresets = dic;
return self;
}
- (ImageResult*) compressImageDimensions:(UIImage*)image
compressImageMaxWidth:(CGFloat)maxWidth
compressImageMaxHeight:(CGFloat)maxHeight
intoResult:(ImageResult*)result {
CGFloat oldWidth = image.size.width;
CGFloat oldHeight = image.size.height;
int newWidth = 0;
int newHeight = 0;
if (maxWidth < maxHeight) {
newWidth = maxWidth;
newHeight = (oldHeight / oldWidth) * newWidth;
} else {
newHeight = maxHeight;
newWidth = (oldWidth / oldHeight) * newHeight;
}
CGSize newSize = CGSizeMake(newWidth, newHeight);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
result.width = [NSNumber numberWithFloat:newWidth];
result.height = [NSNumber numberWithFloat:newHeight];
result.image = resizedImage;
return result;
}
- (ImageResult*) compressImage:(UIImage*)image
withOptions:(NSDictionary*)options {
ImageResult *result = [[ImageResult alloc] init];
result.width = @(image.size.width);
result.height = @(image.size.height);
result.image = image;
result.mime = @"image/jpeg";
NSNumber *compressImageMaxWidth = [options valueForKey:@"compressImageMaxWidth"];
NSNumber *compressImageMaxHeight = [options valueForKey:@"compressImageMaxHeight"];
// determine if it is necessary to resize image
BOOL shouldResizeWidth = (compressImageMaxWidth != nil && [compressImageMaxWidth floatValue] < image.size.width);
BOOL shouldResizeHeight = (compressImageMaxHeight != nil && [compressImageMaxHeight floatValue] < image.size.height);
if (shouldResizeWidth || shouldResizeHeight) {
CGFloat maxWidth = compressImageMaxWidth != nil ? [compressImageMaxWidth floatValue] : image.size.width;
CGFloat maxHeight = compressImageMaxHeight != nil ? [compressImageMaxHeight floatValue] : image.size.height;
[self compressImageDimensions:image
compressImageMaxWidth:maxWidth
compressImageMaxHeight:maxHeight
intoResult:result];
}
// parse desired image quality
NSNumber *compressQuality = [options valueForKey:@"compressImageQuality"];
if (compressQuality == nil) {
compressQuality = [NSNumber numberWithFloat:0.8];
}
// convert image to jpeg representation
result.data = UIImageJPEGRepresentation(result.image, [compressQuality floatValue]);
return result;
}
- (void)compressVideo:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
withOptions:(NSDictionary*)options
handler:(void (^)(AVAssetExportSession*))handler {
NSString *presetKey = [options valueForKey:@"compressVideoPreset"];
if (presetKey == nil) {
presetKey = @"MediumQuality";
}
NSString *preset = [self.exportPresets valueForKey:presetKey];
if (preset == nil) {
preset = AVAssetExportPresetMediumQuality;
}
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:preset];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
handler(exportSession);
}];
}
@end