【问题标题】:Xcode iOS Generate .png file and save it using NSFileManagerXcode iOS 生成 .png 文件并使用 NSFileManager 保存
【发布时间】:2013-04-26 07:48:17
【问题描述】:

我正在制作一个 iOS 应用程序并且我得到了一个 UIImage - 我想将它压缩成 .png 文件并将其保存到应用程序的文档文件夹中 - 我已经有了路径,我所需要的只是如何将 UIImage 转换为 . png 并保存。

谢谢, 马坦。

【问题讨论】:

    标签: ios objective-c nsfilemanager


    【解决方案1】:

    所以代码是:

    UIImage *yourImage = ...; //the image you have
    NSString *targetPath = ...; // something like ~/Library/Documents/myImage.png
    
    [UIImagePNGRepresentation(yourImage) writeToFile:targetPath atomically:YES];
    

    【讨论】:

    • 非常感谢!完美运行!
    • 有什么方法可以压缩png吗?
    • 没有。你可以包括 pngcrush 来源。但框架中没有任何内容
    【解决方案2】:

    对于 PNG:

    UIImagePNGRepresentation
    

    以PNG格式返回指定图像的数据

    NSData * UIImagePNGRepresentation (
       UIImage *image
    );
    

    如果您想要 JPEG:

    UIImageJPEGRepresentation
    

    以 JPEG 格式返回指定图像的数据。

    NSData * UIImageJPEGRepresentation (
       UIImage *image,
       CGFloat compressionQuality
    );
    

    【讨论】:

      【解决方案3】:

      图片压缩形式为JPEG,可以使用不同质量的jpg图片

      // for getting png image
       NSData*theImageData=UIImagePNGRepresentation(theImage);
      // for JPG compression change fill value between less than 1 
       NSData*theImageData=UIImageJPEGRepresentation(theImage, 1);
      // for converting raw data image to UIImage
       UIImage *imageOrignal=[UIImage imageWithData:theImageData];
      // for saving in to photos gallery
       UIImageWriteToSavedPhotosAlbum(imageOrignal,nil,nil,nil);
      

      【讨论】:

        猜你喜欢
        • 2013-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-02
        相关资源
        最近更新 更多