我使用以下代码通过手指移动来描画PNG。有2个UIImage视图。一个位于背景以将背景图像放置在那里。另一个是清晰的UIImage视图,以在其顶部描边PNG图像。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch * touch in touches) {
currentPoint = [touch locationInView:self.view];
lastPoint = [touch previousLocationInView:self.view];
//set up array to make space between PNG images
if (ABS(currentPoint.x-lastPoint.x)>16
|| ABS(currentPoint.y - lastPoint.y) > 13) {
[brushLocations addObject:[NSValue valueWithCGPoint:currentPoint]];
}
[self drawingWithArray];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[brushLocations removeAllObjects];//reset
}
-(void)drawingWithArray{
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
for (int i=0; i<[brushLocations count]; i++) {
CGPoint center =[[brushLocations objectAtIndex:i]CGPointValue];
// bokehImage is UIImage
bokehImage=[bokehImgArray objectAtIndex: i%[bokehImgArray count]];
/// the PNG images are not semi-transparent, even set the alpha is 0.5??
[bokehImage drawAtPoint:center blendMode:kCGBlendModeOverlay alpha:0.5f];
//drawImage is uiimage view on top of background image view for stroke PNG images.
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
现在,我遇到的问题是响应速度很慢。当手指在设备(IPad4)上移动时,PNG图像不会立即显示。
而且,PNG图像不是半透明的。我想“ drawAtPoint .. blendMode .. alpha”的功能可以使图像半透明(设置为0.5 alpha)。
是的,这样的事情应该起作用:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch * touch in touches) {
currentPoint = [touch locationInView:self.view];
lastPoint = [touch previousLocationInView:self.view];
//set up array to make space between PNG images
if (ABS(currentPoint.x-lastPoint.x)>16
|| ABS(currentPoint.y - lastPoint.y) > 13) {
[brushLocations addObject:[NSValue valueWithCGPoint:currentPoint]];
}
// [self drawingWithArray]; // don't call draw routine during touch handler
[self setNeedsDisplay]; // queue the redraw instead
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Not needed here
// [brushLocations removeAllObjects];//reset
}
//-(void)drawingWithArray
- (void)drawRect:(CGRect)rect
{
// CGContext is already set when drawRect is called
// UIGraphicsBeginImageContext(self.view.frame.size);
// [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
[drawImage.image drawInRect:rect];
for (int i=0; i<[brushLocations count]; i++) {
CGPoint center =[[brushLocations objectAtIndex:i]CGPointValue];
// bokehImage is UIImage
bokehImage=[bokehImgArray objectAtIndex: i%[bokehImgArray count]];
// the PNG images are not semi-transparent, even set the alpha is 0.5??
[bokehImage drawAtPoint:center blendMode:kCGBlendModeOverlay alpha:0.5f];
//drawImage is uiimage view on top of background image view for stroke PNG images.
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
}
[brushLocations removeAllObjects];//reset
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句