【问题标题】:iPhone store image from imageview to fileiPhone将图像从imageview存储到文件
【发布时间】:2011-06-06 13:31:47
【问题描述】:

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

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

但现在我想将我的图像存储在某个具有特定名称的目录中,这样我就可以在我的应用程序中再次使用该图像,并且我想将它存储在 sqlite 文件中。

【问题讨论】:

    标签: iphone objective-c sqlite uiimageview uiimagepickercontroller


    【解决方案1】:

    这里有一个简短的记录: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 列。

    【讨论】:

    • iphonesdkarticles.com 已经死了,并且有一个 robots.txt 禁止 archive.org 抓取它。
    【解决方案2】:
    NSString  *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/ImageFileName.jpg"];
    [UIImageJPEGRepresentation(yourUIImageView.image,1.0) writeToFile:path atomically:YES];
    

    文档:UIImageJPEGRepresentation

    如果您正在处理 PNG,还有另一种方法称为 UIImagePNGRepresentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多