如何将图像文件从iOS照片库(ALAssetsLibrary)复制到应用程序的本地目录? [英] How to copy an image file from iOS Photo Library (ALAssetsLibrary) to the local directory of an App?
本文介绍了如何将图像文件从iOS照片库(ALAssetsLibrary)复制到应用程序的本地目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过ALAssetsLibrary从照片库中获取图像:
I can get images from Photo Library through ALAssetsLibrary:
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
// Copy the photo image to the `/Documents` directory of this App here
}
};
void (^assetGroupEnumerator )(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup *group, BOOL *stop){
if (group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
// fetch
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:^(NSError *error) {
NSLog(@"failed");
}];
我想将特定图像复制到本地目录( App_home / Documents
),但我不知道如何通过处理ALAsset对象来完成这项工作。
I want to copy specific images to the local directory (App_home/Documents
), but I don't know how to exactly do this job by handling ALAsset objects.
推荐答案
尝试使用以下代码
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:YourURL resultBlock:^(ALAsset *asset)
{
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
[data writeToFile:photoFile atomically:YES];//you can save image later
}
failureBlock:^(NSError *err)
{
NSLog(@"Error: %@",[err localizedDescription]);
}
];
获取图像文件目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"Your_Image_Name"];
UIImage *myImg = [UIImage imageWithContentsOfFile:newPath]
这篇关于如何将图像文件从iOS照片库(ALAssetsLibrary)复制到应用程序的本地目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文