我使用CIFilters将图像转换为灰度并应用一些图像处理效果。在UIImageView中显示输出有效;图像显示并已按预期进行修改。
但是,每次调用UIImageJPEGRepresentation似乎都不会返回任何数据。它永远不会奏效。 使用原始彩色图像调用UIImageJPEGRepresentation工作正常。
这里发生了什么?为什么显示图像时jpeg转换失败?不抛出任何异常(设置异常断点,未命中)并且控制台中不显示任何消息。
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'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)
}
答案 0 :(得分:27)
UIImageJPEGRepresentation
似乎使用了CGImage
的{{1}}属性。问题是,当您使用UIImage
初始化UIImage
时,该属性为CIImage
。
我的解决方案是在nil
调用之前添加以下块:
(上次更新Swift 4)
UIImageJPEGRepresentation
答案 1 :(得分:10)
我之前遇到过CIImage的问题并解决这个问题,我用CIImage做了一个CGImage,然后用CGImage做了一个UIImage。
答案 2 :(得分:2)
丹尼尔的答案很有效。在他的答案之下转换为在Swift4中使用。
<强> Swift4 强>
<?php
$array = [
[
"poulecode" => 495271,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (0225 Onder 19 competitie (najaar)) )"
], [
"poulecode" => 500027,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (B3200 Zwaluwen jeugdbeker Onder 19 poule) )"
], [
"poulecode" => 524572,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (0227 Onder 19 competitie (voorjaar)) )"
]
];
$keyword = "voorjaar";
// You can filter your array using array_filter function
$result = array_filter( $array, function( $item ) use ( $keyword ) {
return strpos( $item['teamnaam'], $keyword ) !== false;
});
// and then map result to your datastructure
$result_ids = array_map( function($item){
return $item['poulecode'];
}, $result );
var_dump($result_ids);