我正在尝试缩放和平移屏幕上的图像。
这是我的 drawRect:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetShouldAntialias(context, NO);
CGContextScaleCTM (context, senderScale, senderScale);
[self.image drawAtPoint:CGPointMake(imgposx, imgposy)];
CGContextRestoreGState(context);
}
当senderScale
为1.0时,移动图像(imgposx/imgposy
)非常流畅。但是,如果 senderScale 有任何其他值,性能会受到很大影响,并且当我移动它时图像会断断续续。
我正在绘制的图像是一个 UIImage
对象。我用
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
并绘制一个简单的UIBezierPath(stroke)
:
self.image = UIGraphicsGetImageFromCurrentImageContext();
我做错了什么吗?关闭抗锯齿并没有太大改善。
编辑: 我试过这个:
rectImage = CGRectMake(0, 0, self.frame.size.width * senderScale, self.frame.size.height * senderScale);
[image drawInRect:rectImage];
但它和其他方法一样慢。
最佳答案
如果您希望它表现良好,您应该让 GPU 使用 CoreAnimation 来完成繁重的工作,而不是在 -drawRect:
方法中绘制图像。尝试创建一个 View 并执行以下操作:
myView.layer.contents = self.image.CGImage;
然后通过相对于其父 View 操作 UIView 来缩放和平移它。如果您在 -drawRect:
中绘制图像,您将使其完成为每一帧传输图像的艰苦工作。通过 CoreAnimation 只传输一次,然后让 GPU 缩放和平移图层。
关于ios - 在第 4 代 iPad 上缩放图像很慢,是否有更快的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17752967/