UIImageJPEGRepresentation返回nil [英] UIImageJPEGRepresentation returns nil

查看:783
本文介绍了UIImageJPEGRepresentation返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CIFilters将图像转换为灰度并应用一些图像处理效果。在UIImageView中显示输出有效;图像显示并按预期进行了修改。

I'm using CIFilters to convert an image to grayscale and apply some image processing effects. Displaying the output in a UIImageView works; the image displays and has been modified as expected.

但是,每次调用UIImageJPEGRepresentation时都不会返回任何数据。它永远不会奏效。
使用原始彩色图像调用UIImageJPEGRepresentation工作正常。

However, calling UIImageJPEGRepresentation appears to return no data - every time. It never works. Calling UIImageJPEGRepresentation using the original color image works fine.

这里发生了什么?为什么显示图像时jpeg转换失败可以正常工作?不抛出任何异常(设置异常断点,未触发)并且控制台中不显示任何消息。

What's going on here? Why might the jpeg conversion fail when displaying the image works fine? No exceptions are thrown (setting an exception breakpoint, it's not hit) and no messages appear in the console.

let _cicontext = CIContext(options:nil)

// Set up grayscale and blur filters:
let grayIze = CIFilter(name: "CIColorControls")
let blur = CIFilter(name: "CIGaussianBlur")
grayIze.setValue(0, forKey:kCIInputSaturationKey)
grayIze.setValue(0.5, forKey: kCIInputBrightnessKey)
blur.setValue(4, forKey: kCIInputRadiusKey)

// Go!
let originalImage = CIImage(image: colorImageThatDefinitelyExists)
grayIze.setValue(originalImage, forKey: kCIInputImageKey)
blur.setValue(grayIze.outputImage, forKey: kCIInputImageKey)

let output = UIImage(CIImage: blur.outputImage)
let imageData: NSData? = UIImageJPEGRepresentation(output, 1.0) // Returns nil!?

编辑:这是工作代码,基于 Arbitur的回答

Here is the working code, based on Arbitur's answer:

// Define an image context at the class level, which will only be initialized once:
static let imageContext = CIContext(options:nil)

// And here's the updated code in a function:
class func convertToGrayscale(image: UIImage)->UIImage?
{
    // Set up grayscale and blur filters:
    let filter1_grayIze = CIFilter(name: "CIColorControls")
    let filter2_blur = CIFilter(name: "CIGaussianBlur")
    filter1_grayIze.setValue(0, forKey:kCIInputSaturationKey)
    filter1_grayIze.setValue(0.5, forKey: kCIInputBrightnessKey)
    filter2_blur.setValue(4, forKey: kCIInputRadiusKey)

    // Go!
    let originalImage = CIImage(image: image)
    filter1_grayIze.setValue(originalImage, forKey: kCIInputImageKey)
    filter2_blur.setValue(filter1_grayIze.outputImage, forKey: kCIInputImageKey)
    let outputCIImage = filter2_blur.outputImage

    let temp:CGImageRef = imageContext.createCGImage(outputCIImage, fromRect: outputCIImage.extent())
    let ret = UIImage(CGImage: temp)
    return ret
}

// And finally, the function call:
    if let grayImage = ProfileImage.convertToGrayscale(colorImage)
    {
        let imageData: NSData? = UIImageJPEGRepresentation(grayImage, 1.0)
    }


推荐答案

我之前遇到过CIImage的问题并解决这个问题,我用CIImage创建了一个CGImage,然后用CGImage创建了一个UIImage。

Ive had the problem before with CIImage and to work around that I made a CGImage with the CIImage and then a UIImage with the CGImage.

这篇关于UIImageJPEGRepresentation返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆