gpt4 book ai didi

ios - 如何将iOS图片库(ALAssetsLibrary)中的图片文件复制到App本地目录?

转载 作者:可可西里 更新时间:2023-11-01 05:36:04 24 4
gpt4 key购买 nike

我可以通过 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 对象来完成这项工作。

最佳答案

试试下面的代码

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 - 如何将iOS图片库(ALAssetsLibrary)中的图片文件复制到App本地目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com