目标C语言 最快的图像2D旋转iOS



我想实时或接近实时地旋转2D图像(在x/y平面上)(每秒10帧就足够了)。现在的问题是我能以这样的速度旋转多大的图像。我不能自己做实验的原因是我不能确定我做的对不对。我不确定如果我得到全速使用cattransform3rotate, CGAffineTransformMakeRotation或如果我必须去OpenGL。
我打赌硬件的目的是旋转屏幕大小的图像(或更小),而不是全分辨率(3226 × 2448)的图像。不幸的是,我们必须处理比屏幕更大的图像,有时甚至与整个传感器一样大。我已经用cgaffinetransformmakeroation实现了旋转,如果这就是它的全部,那么我可以从小的开始,然后建立-是吗?

UIImage * ImageRotatedByRadians(UIImage *oldImage, CGFloat radians)
{
NSLog( @"nnRotateImage->imageRotatedByRadians: (%f)nn", radians );
// Calculate the size of the rotated view's containing box for our drawing space.
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, oldImage.size.width, oldImage.size.height )];
CGAffineTransform t = CGAffineTransformMakeRotation( radians );
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context.
UIGraphicsBeginImageContext( rotatedSize );
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM( bitmap, rotatedSize.width / 2, rotatedSize.height / 2 );
// Rotate the image context.
CGContextRotateCTM( bitmap, radians );
// Now, draw the rotated/scaled image into the context.
CGContextScaleCTM( bitmap, 1.0, -1.0 );
CGContextDrawImage( bitmap, CGRectMake( -oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage] );
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
UIGraphicsEndImageContext();
return newImage;
}

问题:iphone 5及更新版本上最快的2D旋转机制是什么?

可以以每秒至少10帧的速度旋转的最大图像区域是多少?

我们正在处理256张灰度图像(我认为这应该会加快速度)。

如果答案是OpenGL,我会感激代码或指针到一个地方开始。

所以快速部分是使用UIViews转换旋转图像,但缓慢部分是当你实际将旋转的像素数据写入另一个UIImage

要运行一个连续链接到屏幕刷新的方法,您可以使用CADisplayLink,如下所示

//inside viewDidLoad or something
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)]; //implement a method called render or some other name
displayLink.frameInterval = 2; // 2 = 30fps, 1 = 60fps 3 = 20fps etc basically the frameInterval = 60/frameInterval
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

render:函数中你可以这样做

- (void)render:(CADisplayLink*)displayLink {
    rotatedBoxView.transform = CGAffineTransformMakeRotation( radians );
    //do other image stuff
}

如果你想加快UIImage的创建,我认为你需要找出如何在另一个线程上做到这一点,可以像把它放在dispatch_async块中一样容易,但我有一种感觉,UIGraphicsGetCurrentContext();的东西不能很好地发挥线程

但我认为无论你使用什么方法,OpenGL或其他方法,你的瓶颈只是创建新的UIImage,所以我不会过分担心加速旋转

没有测试任何代码,但可以是一个正确方向的开始

不是一个完整的答案,但希望它能有所帮助

最新更新