-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPHAsset+Utility.m
138 lines (122 loc) · 5.5 KB
/
PHAsset+Utility.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
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
133
134
135
136
137
//
// PHAsset+Utilities.m
//
// Created by Zakk Hoyt on 9/22/14.
// Copyright (c) 2014 Zakk Hoyt. All rights reserved.
//
#import "PHAsset+Utility.h"
@import Photos;
@implementation PHAsset (Utilities)
#pragma mark Public methods
-(void)saveToAlbum:(NSString*)title completionBlock:(PHAssetBoolBlock)completionBlock{
void (^saveAssetCollection)(PHAssetCollection *assetCollection) = ^(PHAssetCollection *assetCollection){
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
[changeRequest addAssets:@[self]];
} completionHandler:^(BOOL success, NSError *error) {
if(success == NO) {
NSLog(@"Failed to add PHAsset to album: %@ error: %@", title, error.localizedDescription);
}
return completionBlock(success);
}];
};
PHAssetCollection *assetCollection = [self albumWithTitle:title];
if(assetCollection){
// Album exists
saveAssetCollection(assetCollection);
} else {
// Need to create album before saving
// Create new album (will create duplicates)
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error creating album: %@", error);
} else {
PHAssetCollection *assetCollection = [self albumWithTitle:title];
saveAssetCollection(assetCollection);
}
}];
}
}
-(void)requestMetadataWithCompletionBlock:(PHAssetMetadataBlock)completionBlock{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHContentEditingInputRequestOptions *editOptions = [[PHContentEditingInputRequestOptions alloc]init];
editOptions.networkAccessAllowed = YES;
[self requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
CIImage *image = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(image.properties);
});
}];
});
}
-(void)updateLocation:(CLLocation*)location creationDate:(NSDate*)creationDate completionBlock:(PHAssetBoolBlock)completionBlock{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest changeRequestForAsset:self];
if(location) assetRequest.location = location;
if(creationDate) assetRequest.creationDate = creationDate;
} completionHandler:^(BOOL success, NSError *error) {
if(success){
completionBlock(YES);
} else {
completionBlock(NO);
}
}];
}
+(void)saveImageToCameraRoll:(UIImage*)image location:(CLLocation*)location completionBlock:(PHAssetAssetBoolBlock)completionBlock{
__block PHObjectPlaceholder *placeholderAsset = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
newAssetRequest.location = location;
newAssetRequest.creationDate = [NSDate date];
placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
} completionHandler:^(BOOL success, NSError *error) {
if(success){
PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
completionBlock(asset, YES);
} else {
completionBlock(nil, NO);
}
}];
}
+(void)saveVideoAtURL:(NSURL*)url location:(CLLocation*)location completionBlock:(PHAssetAssetBoolBlock)completionBlock{
__block PHObjectPlaceholder *placeholderAsset = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
newAssetRequest.location = location;
newAssetRequest.creationDate = [NSDate date];
placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
} completionHandler:^(BOOL success, NSError *error) {
if(success){
PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
completionBlock(asset, YES);
} else {
completionBlock(nil, NO);
}
}];
}
#pragma mark Private helpers
-(PHAssetCollection*)albumWithTitle:(NSString*)title{
// Check if album exists. If not, create it.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", title];
PHFetchOptions *options = [[PHFetchOptions alloc]init];
options.predicate = predicate;
PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:options];
if(result.count){
return result[0];
}
return nil;
}
+(PHAsset*)getAssetFromlocalIdentifier:(NSString*)localIdentifier{
if(localIdentifier == nil){
NSLog(@"Cannot get asset from localID because it is nil");
return nil;
}
PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil];
if(result.count){
return result[0];
}
return nil;
}
@end