我正在我的应用程序中实现“临时”功能。用户划伤屏幕并看到“下方”图像。
关于touchesMoved:我更新了蒙版图像并将其应用于图层。通常代码是这样的:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint cPoint = [touch locationInView:self];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = _maskImage;
// ... add some subviews to imageView corresponding to touch manner
_maskImage = [UIImage imageFromLayer:imageView.layer];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
_maskImageView.image = _maskImage;
_viewWithOurImage.layer.mask = _maskImageView.layer;
}
我使用代码(UIImage上的类别)从CALayer获取UIImage:
+ (UIImage*)imageFromLayer:(CALayer*)layer
{
UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, 0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
此代码在iOS6上完美运行(在iPhone 4s和iPad2上测试),完全没有滞后。
但是当我在iOS7(xcode4或xcode5)上运行它时,它非常缓慢而且有点滞后。我使用了时间分析器,它明确指向renderInContext:row。
然后我尝试了以下代码:
...
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
_maskImage = [UIImage imageFromLayer:imageView.layer];
else
_maskImage = [UIImage imageFromViewIniOS7:imageView];
...
+ (UIImage*)imageFromViewIniOS7:(UIView*)view
{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0);
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationNone);
// actually there is NSInvocation, but I've shortened for example
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
它仍然很慢。在iPhone 4s上测试(与iOS6相同),新iPod5和iPad3。
我做错了什么?显然这是iOS7的问题......
我将不胜感激。
答案 0 :(得分:3)
我建议你尝试其他一些方式,很遗憾地说, touchesMoved 功能在IOS7中运行缓慢,你的代码没有任何问题