JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
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
//
//  PHAsset+Lechange.m
//
//  Created by Zakk Hoyt on 9/22/14.
//  Copyright (c) 2014 Zakk Hoyt. All rights reserved.
//
 
#import "PHAsset+Lechange.h"
#import <Foundation/Foundation.h>
@import Photos;
 
@implementation PHAsset (Lechange)
 
#pragma mark Public methods
 
+(void)saveImageToCameraRoll:(UIImage*)image
                         url:(NSURL *)url
                     success:(void (^)(void))theSuccess
                     failure:(void(^)(NSError *error))theFailure
{
    __block PHObjectPlaceholder *placeholderAsset = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
    } completionHandler:^(BOOL success, NSError *error) {
        if(success){
            [[NSUserDefaults standardUserDefaults] setObject:placeholderAsset.localIdentifier forKey:url.absoluteString];
            [[NSUserDefaults standardUserDefaults] synchronize];
            if (theSuccess) {
                theSuccess();
            }
        } else {
            if(theFailure){
                theFailure(error);
            }
        }
    }];
}
 
+(void)saveVideoAtURL:(NSURL *)url
              success:(void (^)(void))theSuccess
              failure:(void(^)(NSError *error))theFailure
{
    __block PHObjectPlaceholder *placeholderAsset = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCreationRequest *videoRequest = [PHAssetCreationRequest creationRequestForAsset];
        [videoRequest addResourceWithType:PHAssetResourceTypeVideo fileURL:url options:nil];
        placeholderAsset = videoRequest.placeholderForCreatedAsset;
    } completionHandler:^(BOOL success, NSError *error) {
        if(success){
            [[NSUserDefaults standardUserDefaults] setObject:placeholderAsset.localIdentifier forKey:url.absoluteString];
            [[NSUserDefaults standardUserDefaults] synchronize];
            if (theSuccess) {
                theSuccess();
            }
        } else {
            if(theFailure){
                theFailure(error);
            }
        }
    }];
}
 
+(void)deleteFormCameraRoll:(NSURL *)url
                    success:(void (^)(void))theSuccess
                    failure:(void(^)(NSError *error))theFailure
{
    
    PHAsset *asset = [self getAssetFromlocalIdentifier:[[NSUserDefaults standardUserDefaults] objectForKey:url.absoluteString]];
    if (asset) {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            [PHAssetChangeRequest deleteAssets:@[asset]];
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:url.absoluteString];
                [[NSUserDefaults standardUserDefaults] synchronize];
                if (theSuccess) {
                    theSuccess();
                }
            } else {
                if (theFailure) {
                    theFailure(error);
                }
            }
        }];
    } else {
        if (theSuccess) {
            theSuccess();
        }
    }
}
 
#pragma mark - Private helpers
 
+(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