从iOS

时间:2016-02-19 08:56:27

标签: ios objective-c iphone xcode

目前,我正在使用 UIImageWriteToSavedPhotosAlbum 在特定按钮上保存来自特定网址的图片。图像已成功保存。

现在稍后,我想要从相机胶卷相册iOS中同时按下相同的按钮来检索相同的图像。我不想再为该图片调用该网址。

如何使用Objective-C实现此结果?

 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url
                             options:0
                            progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                            }
                           completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                               if (image && finished) {
                                   UIImageWriteToSavedPhotosAlbum(image,self,
                                       @selector(image:didFinishSavingWithError:contextInfo:),
                                                                      nil);
                               }
                           }];

1 个答案:

答案 0 :(得分:0)

您必须使用照片框架进行保存,而不是使用 UIImageWriteToSavedPhotosAlbum

获取图片后,使用照片框架保存它。

__block PHObjectPlaceholder *placeholderAsset;

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

       PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
       placeholderAsset = changeRequest.placeholderForCreatedAsset;

    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"Success");

            // Save Identifier here
            [[NSUserDefaults standardUserDefaults] setObject:placeholderAsset.localIdentifier forKey:@"PhotoIdentifier"];
        }
        else {
            NSLog(@"write error : %@",error);
        }
    }];

// 使用标识符

重新搜索图像
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];

[allPhotos enumerateObjectsUsingBlock:^(PHAsset   * _Nonnull photoAsset, NSUInteger idx, BOOL * _Nonnull stop) {

    NSString *photoIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"PhotoIdentifier"];
    if([photoIdentifier isEqualToString:photoAsset.localIdentifier]){

        [[PHImageManager defaultManager]requestImageForAsset:photoAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage *result, NSDictionary *info){
            if ([info objectForKey:PHImageErrorKey] == nil && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) {

                // image is here as a result parameter
                *stop = YES;
            }
        }];
    }
}];

您可以使用任何其他媒介来存储照片标识符,NSUserDefaults就是一个例子。