首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何旋转CGImageRef?

如何旋转CGImageRef?
EN

Stack Overflow用户
提问于 2013-01-16 05:12:45
回答 1查看 5.1K关注 0票数 0

我有一台UIImage,当我的iPad处于纵向时,它的方向看起来是正确的,但当我获得与它相关的CGImageRef时,CGImageRef逆时针旋转了90度。经过一些谷歌搜索,我了解到这是因为与UIImage不同,CGImageRef没有方向数据。我需要查看和修改图像中的一些像素,目前我正在通过直接访问rawData变量( CGImageRef是一个UIImage*)来执行此操作:

代码语言:javascript
复制
CGImageRef imageRef = [image CGImage];

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); //maybe make ...Gray();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

但是,为了正确地修改存储在rawData中的像素数据,我需要CGImageRef处于正确的方向。如何将CGImageRef顺时针旋转90度,然后访问rawData (像素信息)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-16 05:32:18

试试这个:

代码语言:javascript
复制
CGImageRef imageRef = [sourceImage CGImage];
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);


CGContextRef bitmap;

if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
    bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

} else {
    bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

}   

if (sourceImage.imageOrientation == UIImageOrientationLeft) {
    CGContextRotateCTM (bitmap, radians(90));
    CGContextTranslateCTM (bitmap, 0, -targetHeight);

} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
    CGContextRotateCTM (bitmap, radians(-90));
    CGContextTranslateCTM (bitmap, -targetWidth, 0);

} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
    // NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
    CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
    CGContextRotateCTM (bitmap, radians(-180.));
}

CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14347019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档