iPhone将图像从imageview存储到文件

时间:2011-01-10 14:04:27

标签: iphone objective-c sqlite uiimageview uiimagepickercontroller

在我的iPhone应用程序中,我从iPhone图像库中提取图像以及设备相机, 我在imageView中显示该图像。

我可以将图像存储到iPhone图像库中。

但是现在我想将我的图像存储在具有特定名称的某个目录中,因此我可以在我的应用程序中再次使用该图像,并且还希望将其存储在sqlite文件中。

2 个答案:

答案 0 :(得分:9)

这里有一个简短的写法:http://iosdevelopertips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html

以下是直接从该网站窃取的相关代码:

// Create paths to output images
NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

要将它存储在SQLite数据库中,您将获取生成NSData对象的代码(UIImageJPEGRepresentationUIImagePNGRepresentation)并将它们保存到BLOB列。

答案 1 :(得分:3)

NSString  *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/ImageFileName.jpg"];
[UIImageJPEGRepresentation(yourUIImageView.image,1.0) writeToFile:path atomically:YES];

文档:UIImageJPEGRepresentation

如果您正在处理PNG,则还有另一种名为UIImagePNGRepresentation的方法。

相关问题