[英]Masking two images
我想在iOS中的兩個圖像之間進行選擇性遮罩,類似於Blender中的遮罩功能。 有兩個圖像1和2(調整為相同尺寸)。 最初,只有圖像1是可見的,但是無論用戶何時觸摸圖像1上的任何區域,圖像都將變為透明,並且圖像2在這些區域中變為可見。
我使用帶有觸摸移動功能的核心圖形創建了類似面具的圖像。 無論我觸摸到哪里,它基本上都是全黑圖像,帶有白色部分。 整個Alpha設置為1.0。 我可以將此圖像用作遮罩,並通過實現自己的圖像處理方法(將在每個像素上進行迭代,檢查並根據值進行設置)來執行必要的操作。 現在,此方法將稱為內部觸摸移動,因此我的方法可能會減慢整個過程(特別是對於8MP攝像機圖像)。
我想知道如何通過使用Quartz Core或Core Graphics來實現這些效果,這些效率足以在大圖像中運行。
我到目前為止的代碼:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:staticBG];
UIGraphicsBeginImageContext(staticBG.frame.size);
[maskView.image drawInRect:CGRectMake(0, 0, maskView.frame.size.width, maskView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 0.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
maskView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10)
mouseMoved = 0;
staticBG.image = [self maskImage:staticBG.image withMask:maskView.image];
//maskView.hidden = NO;
}
- (UIImage*) maskImage:(UIImage *)baseImage withMask:(UIImage *)maskImage
{
CGImageRef imgRef = [baseImage CGImage];
CGImageRef maskRef = [maskImage CGImage];
CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
return [UIImage imageWithCGImage:masked];
}
maskImage方法不起作用,因為它根據alpha值創建蒙版圖像。 我通過以下鏈接: 從路徑創建蒙版,但我不明白答案。
首先,我想提一些我希望您已經知道的事情。
我想建議您從相反的角度來看這個問題。
即,與其嘗試在頂部圖像上打個洞,而不是試圖查看底部,不如將底部圖像放在頂部並遮蓋底部圖像,以使用戶的觸摸點覆蓋特定部分的頂部視圖。 ?
我為您提供了一個示例,可讓您在這里>> http://goo.gl/Zlu31T
祝您好運,如果有任何不清楚的地方,請回發。 我確實相信,這樣做有很多更好和優化的方法。 “ _”
由於您是實時進行的,因此建議您在編輯時將其偽造,如果以后需要輸出圖像,則可以對其進行真實遮罩,因為這可能需要一些時間(時間不多,速度不夠快)實時進行)。 通過偽造,我的意思是將image1用作背景,並在其頂部放置隱藏的image2。 用戶觸摸到該點后,將image2 UIImageView的邊界設置為CGRect rect= CGRectMake(touch.x - desiredRect.size.width/2, touch.y - desiredRect.size.height/2, desiredRect.size.width, desiredRect.size.height);
並使其可見。 desiredRect
將是要顯示的image2的一部分。 松開手指后,您可以僅隱藏image2 UIImageView,以便完全可見image1。 如果您當時的目標不是立即輸出圖像,這是我現在可以想到的最快方法。
使用此代碼將有助於掩蓋兩個UIImages
CGSize newSize = CGSizeMake(320, 377);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[ backGroundImageView.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[self.drawImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageData = UIImagePNGRepresentation(newImage);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.