iPhone - CGImageCreateWithImageInRect旋转一些相机胶卷图片 [英] iPhone - CGImageCreateWithImageInRect rotating some camera roll pictures

查看:149
本文介绍了iPhone - CGImageCreateWithImageInRect旋转一些相机胶卷图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究iPhone的pixelate应用程序。因为使用iPhone 4相机拍摄的照片太大,因此在更新像素化图片时应用程序工作速度非常慢,我首先尝试创建图片的图块,然后更新图块而不是孔图片。

I am working on a pixelate application for iPhone. Because the pictures taken with the iPhone 4 camera are too big and therefore the application is working really slow when updating the pixelated picture, I am trying to create tiles of the picture first and just update the tiles not the hole picture.

制作瓷砖时,它适用于以横向模式(2592 x 1936 pxl)拍摄的相机胶卷照片和低分辨率照片,但不适用于以纵向模式拍摄的照片(1936 x 2592) pxl)。

When building the tiles, it works for camera roll picture taken in landscape mode (2592 x 1936 pxl) and with low resolution pictures, but not with picture taken in portrait mode (1936 x 2592 pxl).

从原始图像中切割图块的代码如下所示:

The code for cutting the tiles from the original image looks like this:

for (i = 0; i < NrOfTilesPerHeight; i++) {
  for (j = 0; j < NrOfTilesPerWidth; j++) {
    CGRect imageRect = CGRectMake(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
    CGImageRef image = CGImageCreateWithImageInRect(aux.CGImage, imageRect);
    UIImage *img = [UIImage imageWithCGImage:image];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
  }
}

问题是使用这些瓷砖创建的图像逆时针旋转90度角。

The problem is that the image created with these tiles it is rotated to a 90 degree angle anticlockwise.

非常感谢,
Andrei

Thank you a lot, Andrei

推荐答案

这是因为不考虑imageOrientation。

It is because the imageOrientation isn't taken into account.

有一个类似的问题(从相机中拉出的UIimages还可以旋转UIimage?)我已经稍微修改了代码以使用裁剪图像。

There is a similar question (Resizing UIimages pulled from the Camera also ROTATES the UIimage?) and I've modified the code a bit to work with cropping image.

在这里:

static inline double radians (double degrees) {return degrees * M_PI/180;}

+(UIImage*)cropImage:(UIImage*)originalImage toRect:(CGRect)rect{

    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect);

    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
    CGContextRef bitmap = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    if (originalImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -rect.size.height);

    } else if (originalImage.imageOrientation == UIImageOrientationRight) {
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -rect.size.width, 0);

    } else if (originalImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (originalImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, rect.size.width, rect.size.height);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, rect.size.width, rect.size.height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);

    UIImage *resultImage=[UIImage imageWithCGImage:ref];
    CGImageRelease(imageRef);
    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return resultImage;
}

这篇关于iPhone - CGImageCreateWithImageInRect旋转一些相机胶卷图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆