从IOS上的UIView将图像保存到应用程序文档文件夹

从IOS上的UIView将图像保存到应用程序文档文件夹,第1张

从IOS上的UIView将图像保存到应用程序文档文件夹

很好,伙计。不要伤害自己或他人。

您可能不希望将这些图像存储在Core Data中,因为如果数据集太大,这会影响性能。最好将图像写入文件。

NSData *pngData = UIImagePNGRepresentation(image);

这会提取您捕获的图像的PNG数据。从这里,您可以将其写入文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSdocumentDirectory, NSUserDomainMask, YES);  NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name[pngData writeToFile:filePath atomically:YES]; //Write the file

以后阅读它的方式相同。像上面一样构建路径,然后:

NSData *pngData = [NSData dataWithContentsOfFile:filePath];UIImage *image = [UIImage imageWithdata:pngData];

您可能想要做的是创建一个为您创建路径字符串的方法,因为您不希望该代码随处可见。它可能看起来像这样:

- (NSString *)documentsPathForFileName:(NSString *)name{    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSdocumentDirectory,NSUserDomainMask, YES);      NSString *documentsPath = [paths objectAtIndex:0];    return [documentsPath stringByAppendingPathComponent:name]; }

希望对您有所帮助。



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

原文地址: https://outofmemory.cn/zaji/5642184.html

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

发表评论

登录后才能评论

评论列表(0条)

保存