【发布时间】:2011-09-18 19:44:08
【问题描述】:
如何旋转上下文?
我已经尝试了CGAffineTransformRotate() 和CGContextRotateCTM() 的所有组合,我能想到但无法实现。
下面的代码运行良好。它使用背景捕捉各种尺寸的图像,因此它们始终为 320x480 或 480x320。这很重要。但是,我希望水平图像在保存到文件之前旋转 90 度。
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 320), NO, 0.0);
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
UIImage *im = [UIImage imageWithContentsOfFile:[allImagePaths objectAtIndex:currImg]];
iv.image = im;
iv.backgroundColor = [UIColor blackColor];
iv.contentMode = UIViewContentModeScaleAspectFit;
CGContextRef context = UIGraphicsGetCurrentContext();
[iv.layer renderInContext:context];
//
// how to rotate it around here?
//
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
NSData* imageData = UIImageJPEGRepresentation(capturedImage, 1.0);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[imageData writeToFile:[docDir stringByAppendingPathComponent:@"result.jpg"] atomically:NO];
UIGraphicsEndImageContext();
每当我添加任何类型的旋转时,它都会出现问题(白色结果图像或屏幕上只有一半图像且未旋转)。
对这个例子的任何帮助都会很好。
【问题讨论】:
标签: ios rotation uiimage transform cgcontext