Objective c IOS Objective C保持定向但旋转图像数据

Objective c IOS Objective C保持定向但旋转图像数据,objective-c,rotation,uiimage,orientation,Objective C,Rotation,Uiimage,Orientation,真的希望你能帮上忙整天都在烦我 是否可以保持UIImage的方向(例如横向)但在内部旋转图像 我使用以下代码保存UIImage [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; 其中image是UIImage 下图显示了根据设备方向保存的内容 下面四个是我想要达到的目标 基本上,我正在尝试让图片在横向模式下处于其一侧,无论您以何种方式转动设备 谢谢你的帮助 标记试试这个 UIImage* landsca

真的希望你能帮上忙整天都在烦我

是否可以保持UIImage的方向(例如横向)但在内部旋转图像

我使用以下代码保存UIImage

[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
其中image是UIImage

下图显示了根据设备方向保存的内容

下面四个是我想要达到的目标

基本上,我正在尝试让图片在横向模式下处于其一侧,无论您以何种方式转动设备

谢谢你的帮助

标记

试试这个

UIImage* landscapeImage = [UIImage imageWithCGImage:image.CGImage
                                            scale:image.scale
                                      orientation:UIImageOrientationLeft];
[UIImagePNGRepresentation(landscapeImage) writeToFile:pngPath atomically:YES];
试试这个

UIImage* landscapeImage = [UIImage imageWithCGImage:image.CGImage
                                            scale:image.scale
                                      orientation:UIImageOrientationLeft];
[UIImagePNGRepresentation(landscapeImage) writeToFile:pngPath atomically:YES];

我通常只保存图像文件,然后在UI中更改imageView。只要图像始终处于纵向模式,您可以先将图像视图旋转90度,如下所示:

self.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
然后将imageView模式设置为Aspect Fit

self.imageView.contentMode = UIViewContentModeScaleAspectFit;

但是,如果您的图像需要以另一种方式旋转,或者图像已经定位在横向中,则此操作将不起作用

我会正常保存图像文件,然后在UI中更改imageView。只要图像始终处于纵向模式,您可以先将图像视图旋转90度,如下所示:

self.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
然后将imageView模式设置为Aspect Fit

self.imageView.contentMode = UIViewContentModeScaleAspectFit;

但是,如果您的图像需要以另一种方式旋转,或者图像已经定位在横向中,则此操作将不起作用

它有助于固定拍摄图像的方向吗

如果没有,也许你可以为自己的问题找到灵感。基本上创建具有正确宽度/高度的上下文、旋转变换、绘制图像、存储为新图像

CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
    CGImageGetBitsPerComponent(image.CGImage), 0,
    CGImageGetColorSpace(image.CGImage),
    CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
//or
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);

CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
还可以阅读有关相机和多媒体资料中不同结果的评论


如果只想旋转显示,可以像@bryannorden建议的那样变换UIView。

这有助于固定拍摄图像的方向吗

NSBitmapImageRep *bit_map = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes:NULL
                             pixelsWide:img_size.width
                             pixelsHigh:img_size.height
                             bitsPerSample:8
                             samplesPerPixel:4
                             hasAlpha:YES
                             isPlanar:NO
                             colorSpaceName:NSDeviceRGBColorSpace
                             bitmapFormat:NSAlphaFirstBitmapFormat
                             bytesPerRow:0
                             bitsPerPixel:0];
NSAffineTransform *trans2=[NSAffineTransform transform];
[trans2 translateXBy:dirtyRect.size.width/2 yBy:dirtyRect.size.height/2];
[trans2 rotateByDegrees: angle];
[trans2 concat];

NSGraphicsContext *g=[NSGraphicsContext graphicsContextWithBitmapImageRep:bit_map];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
[img drawInRect:NSMakeRect(0, 0, img_size.width, img_size.height)];
[NSGraphicsContext restoreGraphicsState];


[bit_map drawInRect:NSMakeRect(-img_size.width/2, -img_size.height/2, img_size.width, img_size.height)];

如果没有,也许你可以为自己的问题找到灵感。基本上创建具有正确宽度/高度的上下文、旋转变换、绘制图像、存储为新图像

CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
    CGImageGetBitsPerComponent(image.CGImage), 0,
    CGImageGetColorSpace(image.CGImage),
    CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
//or
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);

CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
还可以阅读有关相机和多媒体资料中不同结果的评论

如果只想显示旋转视图,可以像@bryannorden建议的那样变换UIView

NSBitmapImageRep *bit_map = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes:NULL
                             pixelsWide:img_size.width
                             pixelsHigh:img_size.height
                             bitsPerSample:8
                             samplesPerPixel:4
                             hasAlpha:YES
                             isPlanar:NO
                             colorSpaceName:NSDeviceRGBColorSpace
                             bitmapFormat:NSAlphaFirstBitmapFormat
                             bytesPerRow:0
                             bitsPerPixel:0];
NSAffineTransform *trans2=[NSAffineTransform transform];
[trans2 translateXBy:dirtyRect.size.width/2 yBy:dirtyRect.size.height/2];
[trans2 rotateByDegrees: angle];
[trans2 concat];

NSGraphicsContext *g=[NSGraphicsContext graphicsContextWithBitmapImageRep:bit_map];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
[img drawInRect:NSMakeRect(0, 0, img_size.width, img_size.height)];
[NSGraphicsContext restoreGraphicsState];


[bit_map drawInRect:NSMakeRect(-img_size.width/2, -img_size.height/2, img_size.width, img_size.height)];
在行动中看到它:


看到它的行动:

对不起,它什么也没做。我把它应用到原始图像上,它是横向的,里面的图像是颠倒的,它用相同的图像拍摄回来。我需要它向右旋转90度,但保持景观。上面的代码只会将任何方向更改为横向左侧。如果你说的是旋转图像,那么请参考链接,谢谢,我看到一个长约100,所有的旋转图像到我想要的,但所有的旋转方向,以及或挤压图像到合适的我想我可能能够修改一个你给的名为RotateImage的链接,它只需要图像和旋转帮助解决如何裁剪图像以适应景观我认为这两条线是关键UIView*rotatedViewBox=[[UIView alloc]initWithFrame:CGRectMake(0,0,image.size.width,image.size.height)];CGContextDrawImage(位图,CGRectMake(-image.size.width,-image.size.height,image.size.width,image.size.height),[image CGImage]);我认为第一条线设置了要从原始图像中抓取的区域,第二条线将其绘制到新图像,但无法解决该如何操作。抱歉,它什么也没做。我将其应用到原始图像中,图像内部颠倒,并使用相同的图像进行拍摄。我需要它向右旋转90度,但保持不变景观上述代码仅将任何方向更改为景观左侧。如果你说的是旋转图像,那么请参考链接,谢谢,我看到一个长约100,所有的旋转图像到我想要的,但所有的旋转方向,以及或挤压图像到合适的我想我可能能够修改一个你给的名为RotateImage的链接,它只需要图像和旋转帮助解决如何裁剪图像以适应景观我认为这两条线是关键UIView*rotatedViewBox=[[UIView alloc]initWithFrame:CGRectMake(0,0,image.size.width,image.size.height)];CGContextDrawImage(位图,CGRectMake(-image.size.width,-image.size.height,image.size.width,image.size.height),[image CGImage]);我认为第一条线设置了要从原始图像中抓取的区域,第二条线将其绘制到新图像,但无法解决如何做。我已经嵌入了您的图像,现在我看到了它,您总是希望看到肖像中的人物,无论它是以什么方向拍摄的。因此,您需要在旋转之前(或之后)裁剪横向图像。在我的示例中,您还可以更改CGRectMake,只需从负坐标开始,并使其在画布外绘制。如果您使用ImageView,您将需要剪裁,请参见此处:已嵌入图像,现在我看到了它,您始终希望肖像中的人物,无论其方向如何。因此,您需要在旋转之前(或之后)裁剪横向图像。在我的示例中,您还可以更改CGRectMake,只需从负坐标开始,并使其在画布外绘制。如果使用ImageView,则需要剪裁,请参见此处: