-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathViewController.m
More file actions
92 lines (78 loc) · 3.4 KB
/
Copy pathViewController.m
File metadata and controls
92 lines (78 loc) · 3.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
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
//
// ViewController.m
// OTLargeImageReaderDemo
//
// Created by 史江浩 on 6/1/15.
// Copyright (c) 2015 openthread. All rights reserved.
//
#import "ViewController.h"
#import "OTLargeImageReader.h"
#import "UIImage+TraditionalCompress.h"
@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
@implementation ViewController
{
UIImageView *_backgroundImageView;
UIButton *_imageReaderButton;
UIButton *_imageContextButton;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:_backgroundImageView];
_imageReaderButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_imageReaderButton setTitle:@"Compress image using OTLargeImageReader" forState:UIControlStateNormal];
[_imageReaderButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_imageReaderButton addTarget:self action:@selector(largeImageReaderButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_imageReaderButton];
_imageContextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_imageContextButton setTitle:@"Compress image using CGContext" forState:UIControlStateNormal];
[_imageContextButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_imageContextButton addTarget:self action:@selector(contextButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_imageContextButton];
}
- (void)largeImageReaderButtonTouched:(id)sender
{
NSString *imagePath = [[self class] imagePath];
UIImage *thumbnail = [OTLargeImageFileReader thumbImageFromLargeFile:imagePath
withMinPixelSize:1080
imageSize:CGSizeZero];
_backgroundImageView.image = thumbnail;
}
- (void)contextButtonTouched:(id)sender
{
UIImage *image = [UIImage imageWithContentsOfFile:[[self class] imagePath]];
UIImage *compressedImage = [image imageByScalingProportionallyToSize:CGSizeMake(1080, 1080)];
_backgroundImageView.image = compressedImage;
image = nil;
}
- (void)viewDidLayoutSubviews
{
_backgroundImageView.frame = self.view.bounds;
_imageReaderButton.frame = CGRectMake(0,
0,
CGRectGetWidth(self.view.frame),
CGRectGetHeight(self.view.frame) / 2);
_imageContextButton.frame = CGRectMake(0,
CGRectGetHeight(self.view.frame) / 2,
CGRectGetWidth(self.view.frame),
CGRectGetHeight(self.view.frame) / 2);
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[_imageReaderButton setImage:image forState:UIControlStateNormal];
}
+ (NSString *)imagePath
{
static NSString *cachePath = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cachePath = [[NSBundle mainBundle] pathForResource:@"haruhi_suzumiya_with_long_hair" ofType:@"png"];
});
return cachePath;
}
@end