如何将图像文件从iOS Photo Library(ALAssetsLibrary)复制到App的本地目录?

如何将图像文件从iOS Photo Library(ALAssetsLibrary)复制到App的本地目录?,第1张

概述我可以通过ALAssetsLibrary从照片库中获取图像: void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){ if([[result valueForProperty:ALAssetPropertyType] isEq 我可以通过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]);    }];

用于获取Image In文档目录

NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);Nsstring *documentsDirectory = [paths objectAtIndex:0];Nsstring *newPath = [documentsDirectory stringByAppendingPathComponent:@"Your_Image_name"];UIImage *myimg = [UIImage imageWithContentsOffile:newPath]
总结

以上是内存溢出为你收集整理的如何将图像文件从iOS Photo Library(ALAssetsLibrary)复制到App的本地目录?全部内容,希望文章能够帮你解决如何将图像文件从iOS Photo Library(ALAssetsLibrary)复制到App的本地目录?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1054584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存