我在UITableView中显示非常大的图像,因此我的应用程序在一段时间后崩溃。现在我想调整图像大小并将其保存到磁盘。有人可以帮助我调整NSData/UIImage的图像大小并保存到磁盘。从iPhone的UIImage将图像保存到沙盒中
我得到的代码来调整图像从UIImage,所以结果我有我在UIImage对象的图像调整大小,如何将它保存到iphone沙箱。
谢谢,
我在UITableView中显示非常大的图像,因此我的应用程序在一段时间后崩溃。现在我想调整图像大小并将其保存到磁盘。有人可以帮助我调整NSData/UIImage的图像大小并保存到磁盘。从iPhone的UIImage将图像保存到沙盒中
我得到的代码来调整图像从UIImage,所以结果我有我在UIImage对象的图像调整大小,如何将它保存到iphone沙箱。
谢谢,
你在问'我该怎么写文件'吧?
我想你可能想写入Library/Caches目录中有一些复杂因素,所以当手机备份时你不保存这些文件。
获取应用程序文件夹的根w/NSHomeDirectory()
,然后附加库/缓存。
您可以在NSData上的fs w/a方法中编写一个NSData。如果您有UIImage,则可以使用UIImageJPEGRepresentation()
或UIImagePNGRepresentation
来获取数据。
NSData *imgData = UIImageJPEGRepresentation(image, 1);
CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imgData, NULL);
NSDictionary *metadata = [(NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[imageMutableData writeToFile:jpgPath atomically:YES];
将您的图像复制到文件夹中的沙箱名称Test.jpg
。
如果你想这样做对PNG,你可以尝试UIImagePNGRepresentation(image);
这里是代码保存到iOS上的文件目录是从工作中。
// 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];
真棒答案,它帮助了我。这是简单的(一行代码)再次访问该数据?或者我应该问一个关于堆栈的问题? – KKendall 2013-05-21 03:21:08
你可能想使用 的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES); NSString * cachesDirectory = [paths objectAtIndex:0]; 而不是自己附加“Library/Caches”。 – 2010-03-31 12:05:55