2016-11-17 115 views
1

我已創建此類別以將CIImage轉換爲UIImage。我使用的CIImage來自CIFilter s。CIImage大小範圍零?

-(UIImage *)obtainImageWithRetinaScale:(CGFloat)scale 
{ 
    CIContext *context = [CIContext contextWithOptions:nil]; 

    //CGRect frame = CGRectMake(0,0,700,392); 
    CGImageRef processedCGImage = [context createCGImage:self 
               fromRect:[self extent]]; 

    UIImage *returnImage = [UIImage imageWithCGImage:processedCGImage 
            scale:scale 
           orientation:UIImageOrientationUp]; 
    CGImageRelease(processedCGImage); 

    return returnImage; 
} 

在運行此,[self extent]給人以亂碼數字的矩形,是勉強(0,0,無窮大,無窮)......真的很龐大的數字在那裏。

顯然處理的圖像是零。

當我強制正確尺寸的框架時,圖像被正確生成。

+0

您是否編寫自己的內核? http://benbritten.com/2008/07/24/cifilter-infinite-extent-problems/ – HAS

+0

或者這有幫助嗎? http://stackoverflow.com/a/21787766/1489885 – HAS

回答

1

使用下面的代碼,學分完全去William Vasconcelos在他的回答here這就是爲什麼我提出這個社區的wiki:

//assume beginImage is CIImage you want to tint    
CIImage* outputImage = nil; 

//create some blue 
CIFilter* blueGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"]; 
CIColor* blue = [CIColor colorWithString:@"0.1 0.5 0.8 1.0"]; 
[blueGenerator setValue:blue forKey:@"inputColor"]; 
CIImage* blueImage = [blueGenerator valueForKey:@"outputImage"]; 

//apply a multiply filter 
CIFilter* filterm = [CIFilter filterWithName:@"CIMultiplyCompositing"]; 
[filterm setValue:blueImage forKey:@"inputImage"]; 
[filterm setValue:beginImage forKey:@"inputBackgroundImage"]; 
outputImage = [filterm valueForKey:@"outputImage"]; 

要帶來一些個人的貢獻,使值得的答案,這裏是Swift 3版本:

// assume beginImage is the CIImage you want to tint 
let outputImage: CIImage? 
let blueGenerator = CIFilter(name: "CIConstantColorGenerator", withInputParameters: ["inputColor": CIColor(string: "0.1 0.5 0.8 1.0")]) 
let blueImage = blueGenerator!.outputImage 

let filterM = CIFilter(name: "CIMultiplyCompositing", withInputParameters: ["inputImage": blueImage, "inputBackgroundImage": beginImage]) 
outputImage = filterM.outputImage