forked from thisandagain/cam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImage+Save.m
56 lines (43 loc) · 1.58 KB
/
UIImage+Save.m
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
//
// UIImage+Save.m
// DIYCam
//
// Created by Andrew Sliwinski on 6/21/12.
// Copyright (c) 2012 DIY, Co. All rights reserved.
//
#import "UIImage+Save.h"
@implementation UIImage (Save)
#pragma mark - Public methods
- (NSURL *)saveToTemporary
{
NSData *imageData = UIImageJPEGRepresentation(self, 0.9);
NSString *assetPath = [self createAssetFilePath:0 withExtension:@"jpg"];
[imageData writeToFile:assetPath atomically:true];
return [NSURL URLWithString:assetPath];
}
- (NSURL *)saveToCache
{
NSData *imageData = UIImageJPEGRepresentation(self, 0.9);
NSString *assetPath = [self createAssetFilePath:1 withExtension:@"jpg"];
[imageData writeToFile:assetPath atomically:true];
return [NSURL URLWithString:assetPath];
}
#pragma mark - Private methods
- (NSString *)createAssetFilePath:(int)type withExtension:(NSString *)extension
{
NSArray *paths = nil;
NSString *documentsDirectory = nil;
switch (type) {
case 0:
documentsDirectory = NSTemporaryDirectory();
break;
default:
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
break;
}
NSString *assetName = [NSString stringWithFormat:@"%@.%@", [[NSProcessInfo processInfo] globallyUniqueString], extension];
NSString *assetPath = [documentsDirectory stringByAppendingPathComponent:assetName];
return assetPath;
}
@end