iPhone - CGImageCreateWithImageInRect 旋转一些相机胶卷图片

发布于 2024-10-18 13:09:31 字数 690 浏览 4 评论 0原文

我正在为 iPhone 开发一个像素化应用程序。由于用 iPhone 4 相机拍摄的照片太大,因此应用程序在更新像素化图片时运行速度非常慢,我尝试先创建图片的图块,然后只更新图块而不是孔图片。

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

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

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 度角。

非常感谢, 安德烈

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.

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];
  }
}

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

Thank you a lot,
Andrei

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2024-10-25 13:09:31

这是因为没有考虑 imageOrientation。

有一个类似的问题(调整从相机中提取的 UIimages 的大小还旋转 UIimage?),我对代码进行了一些修改以处理裁剪图像。

给你:

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;
}

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

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.

Here you are:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文