1
我需要将图像旋转90度,即交换宽度和高度(翻转图像)。在将图像提供给编码器以创建视频之前,需要完成此操作。我用下面的代码(参考:How do I export UIImage array as a movie?)CGAffineTransform旋转90度
- (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, CGImageGetWidth(image),
CGImageGetHeight(image), kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
&pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, CGImageGetWidth(image),
CGImageGetHeight(image), 8, 4*CGImageGetWidth(image), rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(1.571)); // ROTATION
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
然而,通过90度的旋转不工作(通过评论表示)。你能找出问题吗?
感谢您的回复。我明白你的意思,但该方法仍然无效。此外,frameSize本身就是图像大小。对不起,我已经更新了代码。 –
这是我使用的代码: CGContextTranslateCTM(context,CGImageGetHeight(image)/ 2,CGImageGetWidth(image)/ 2); CGContextRotateCTM(context,M_PI_2); CGContextTranslateCTM(context,-CGImageGetWidth(image)/ 2,-CGImageGetHeight(image)/ 2); 图像旋转但在旋转时切出一半。 –
添加完整的代码。一探究竟。创建上下文时,您必须已经混淆了大小。 –