在iPhone中从UIImage将图像保存到沙箱

nbo*_*jja 12 iphone uiimage

我在UITableView中显示非常大的图像,因为这个我的应用程序在一段时间后崩溃了.现在我想调整图像大小并将其保存到磁盘.有人可以帮助我从NSData/UIImage调整图像大小并保存将其保存到磁盘.

我得到了从UIImage调整图像大小的代码,因此我在UIImage对象中调整了图像大小,如何将其保存到iphone沙箱中.

谢谢,

Pra*_*les 30

以下是保存到iOS上来自工作代码的Documents目录的代码.

// Convert UIImage to JPEG
NSData *imgData = UIImageJPEGRepresentation(image, 1); // 1 is compression quality

// Identify the home directory and file name    
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 

// Write the file.  Choose YES atomically to enforce an all or none write. Use the NO flag if partially written files are okay which can occur in cases of corruption
[imgData writeToFile:jpgPath atomically:YES]; 
Run Code Online (Sandbox Code Playgroud)


And*_*iot 4

您是在问“我如何编写文件”吗?

我想这有点复杂,因为您可能想写入 Library/Caches 目录,因此当手机备份时,您不会保存这些文件。

获取应用程序文件夹的根目录NSHomeDirectory(),然后附加库/缓存。

您可以使用 NSData 上的方法将 NSData 写入文件系统。如果你有一个 UIImage,你可以执行UIImageJPEGRepresentation()UIImagePNGRepresentation来获取数据。

  • 您可能想使用 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDirectory = [路径objectAtIndex:0]; 而不是自己添加“库/缓存”。 (6认同)