将所选图像添加到ALAssetsLibrary

时间:2015-04-13 06:54:05

标签: ios objective-c alassetslibrary

我正在尝试将选定的图像从数组添加到ALAssetsLibrary,但他们正在复制图像而不是移动到另一个库..

我如何移动到另一个图书馆?

我的代码在这里:

ALAssetsLibrary* lib = [ALAssetsLibrary new];
for (int i=0; i<self.deletegalleryImages.count; i++) {
    UIImage *image = [self.deletegalleryImages objectAtIndex:i];
    [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:@{} completionBlock:^(NSURL *assetURL, NSError *error) {
        NSLog(@"Write image %@ to asset library. (Error %@)", assetURL, error);
    }];
}

1 个答案:

答案 0 :(得分:0)

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html

此错误代码对应于NSFileReadUnsupportedSchemeError常量 - i。即您不能只使用assets-library:// URL来使用NSFileManager移动文件。 (关于ipod-library:// URL的情况也是如此。)您必须使用AssetsLibrary framework获取文件的数据,然后使用 - [NSData writeToFile:atomically:]将其写入文件。

ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib assetForUrl:theURL // the asset URL you have obtained, NSURL object
    resultBlock:^(ALAsset *asset) {
        // get data
        ALAssetRepresentation *repr = [asset defaultRepresentation];
        CGImageRef cgImg = [repr fullResolutionImage];
        NSString *fname = [repr fileName];
        UIImage *img = [UIImage imageWithCGImage:cgImg];
        NSData *data = UIImagePNGRepresentation(img);
        [data writeToFile:[@"BaseDirectory/" stringByAppendingPathComponent:fname]
            atomically:YES];
        [lib release];
    }
    failureBlock:^(NSError *error) {
        // recover from error, then
        [lib release];
    }];