2014-06-07 42 views
0

我試圖使用CIFilter對圖像應用各種效果(亮度,對比度,飽和度等)。iOS:圖像濾鏡CIFilter狀態未維護

圖像過濾器已成功應用。但是,編輯後圖像的最後狀態不會保留,例如:如果我增加圖像的亮度,然後將對比度應用於編輯後的圖像,則將對比度應用於原始圖像,而不是應用於亮度增加圖像

以下是代碼:

- (IBAction)contrastSliderValueChanged:(id)sender { 

     filter=NULL; 
     result=NULL; 
     cgimg=NULL; 
     newimg=NULL; 



     if (adjustMenuTag==1) { 

      filter=[CIFilter filterWithName:@"CIColorControls"]; 
      [filter setDefaults]; 
      [filter setValue:beginImage forKey:@"inputImage"]; 
      [filter setValue:[NSNumber numberWithFloat:contrastSlider.value] forKey:@"inputBrightness"] ; 
      result=[filter valueForKey:kCIOutputImageKey]; 

      cgimg=[context createCGImage:result fromRect:[result extent]]; 
      newimg=[UIImage imageWithCGImage:cgimg]; 


      imageViewDisplay.image=newimg; 
      beginImage = result; 
      CFRelease(cgimg); 
    //  beginImage = [[CIImage alloc] initWithImage:imageViewDisplay.image]; 


     } else if(adjustMenuTag ==2) { 

      filter=[CIFilter filterWithName:@"CIColorControls"]; 
      [filter setDefaults]; 
      [filter setValue:beginImage forKey:@"inputImage"]; 
      [filter setValue:[NSNumber numberWithFloat:contrastSlider.value+0.5] forKey:@"inputContrast"] ; 
      result=[filter valueForKey:kCIOutputImageKey]; 
      cgimg=[context createCGImage:result fromRect:[result extent]]; 
      newimg=[UIImage imageWithCGImage:cgimg]; 
      imageViewDisplay.image=newimg; 
      beginImage = result; 
      CFRelease(cgimg); 
    //  beginImage = newimg.CIImage; //this also doesn't work. 
     } 
} 

我要去哪裏錯了?我該如何解決這個問題?如何維護編輯過的圖像的狀態?

回答

0

看看代碼,看起來您每次選擇菜單項時都會創建一個CIColorControls過濾器的新實例。你應該創建一個,保持它,並根據需要更新它的輸入。