2012-10-18 68 views
2

中移動的觸摸我正在使用混合模式目的地刪除圖像。我成功地做到了。如何重新繪製一個已刪除的圖像與iPhone

其實我減少了觸摸的阿爾法,所以我也能夠設置實力。
現在我的問題是關於重新繪製圖像的擦除部分與觸摸(即我想用力量拉回圖像或想設置alpha更暗)。爲此,我對原始圖像進行備份,然後裁剪觸摸部分並將其與圖像合併。但問題是,它正在繪製超過它應該。

請注意,繪製重疊(需要設置上限)時,重繪過程只會使圖像變暗得比原始圖像更多。那麼,如何避免在已經繪製圖像的位置重新繪製圖像,以避免原始圖像變暗。

我也附上了代碼。

// Code to erase an image 


     UIGraphicsBeginImageContext(self._overlayImage.image.size); 
     CGRect rect =CGRectMake(0, 0, self._overlayImage.image.size.width, self._overlayImage.image.size.height) ; 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGImageRef imageRef=self._overlayImage.image.CGImage; 

     if (imageRef) { 
      // Restore the screen that was previously saved 
      CGContextTranslateCTM(context, 0, rect.size.height); 
      CGContextScaleCTM(context, 1.0, -1.0); 

      CGContextDrawImage(context, rect, imageRef); 
      //CGImageRelease(imageRef); 

      CGContextTranslateCTM(context, 0, rect.size.height); 
      CGContextScaleCTM(context, 1.0, -1.0); 
     } 

     // Erase the background -- raise the alpha to clear more away with eash swipe 
     // [[UIImage imageNamed:@"eraser222.png"] drawAtPoint:point blendMode:kCGBlendModeDestinationOut alpha:.2]; 
     [ [UIImage imageNamed:@"eraser222.png"] drawInRect:CGRectMake(newPoint.x-self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, newPoint.y-self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height, self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height) blendMode:kCGBlendModeDestinationOut alpha:strength/3]; 

     self._overlayImage.image=UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 


// code to draw an image 

UIImage *cropped = [self imageByCropping:self.imgOrignal toRect:CGRectMake(newPoint.x-self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, newPoint.y-self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height, self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height)]; 
UIGraphicsBeginImageContext(self._overlayImage.image.size); 
     CGRect rect =CGRectMake(0, 0, self._overlayImage.image.size.width, self._overlayImage.image.size.height) ; 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGImageRef imageRef=self._overlayImage.image.CGImage; 

     if (imageRef) { 
      // Restore the screen that was previously saved 
      CGContextTranslateCTM(context, 0, rect.size.height); 
      CGContextScaleCTM(context, 1.0, -1.0); 

      CGContextDrawImage(context, rect, imageRef); 
      //CGImageRelease(imageRef); 

      CGContextTranslateCTM(context, 0, rect.size.height); 
      CGContextScaleCTM(context, 1.0, -1.0); 
     } 




     [ cropped drawInRect:CGRectMake(newPoint.x-self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, newPoint.y-self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height, self.imgOrignal.size.width*2*radius/self._overlayImage.bounds.size.width, self.imgOrignal.size.height*2*radius/self._overlayImage.bounds.size.height) blendMode:kCGBlendModeNormal alpha:strength]; 



     cropped= [UIImage imageWithData:UIImagePNGRepresentation(cropped)]; 


     UIImage *finalimage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 


     self._overlayImage.image=finalimage; 

回答

1

我知道爲時已晚在這裏ANS,但我已經達到了一個解決方案,我會喜歡在這裏分享,因爲它可以幫助有需要。無法刪除和重繪相同的圖像。所以我研究了兩個圖像。原來的圖像和第二張圖像是零。第二張圖像用於繪製用戶觸摸屏幕的路徑。此後創建了一個新的上下文,繪製了原始圖像,然後用kCGBlendModeDestinationOut混合模式繪製了路徑圖像。這是kCGBlendModeDestinationOut這個方法的主要英雄。主要任務是通過使用kCGBlendModeDestinationOut混合模式完成的。因此獲得所需的效果。檢查我的博客here

相關問題