如何从文件中旋转和裁剪 iOS CGImage
Posted
技术标签:
【中文标题】如何从文件中旋转和裁剪 iOS CGImage【英文标题】:How to rotate and crop iOS CGImage from file 【发布时间】:2015-08-11 15:11:29 【问题描述】:我在 ios 中的图像旋转遇到了一些问题。我正在应用程序的背景中执行一些图像处理......我想旋转和裁剪图像。目前,旋转似乎工作正常,但无论我尝试了什么,作物都关闭了。我在 GIMP 中执行了这些相同的操作,发现图像裁剪正确,所以我相信它与 Quartz 坐标系有关。此外,任一方向的弧度越大,作物就越“偏离”。这是我用来旋转和裁剪的代码:
+(UIImage*)imageWithImageFile:(NSString*)imageFile
radians:(float)radians
imageSize:(CGSize)imageSize
croppedToRect:(CGRect)croppedToRect
UIImage* returnImg = nil;
@autoreleasepool
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);
CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, YES, kCGRenderingIntentDefault);
UIGraphicsBeginImageContext(croppedToRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextTranslateCTM(context, imageSize.width * 0.5,
imageSize.height * 0.5);
CGContextRotateCTM(context, radians);
CGContextTranslateCTM(context, -imageSize.width * 0.5, -imageSize.height * 0.5);
//Translate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(context, 0.0, imageSize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, (CGRect) croppedToRect.origin, imageSize, image);
returnImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGDataProviderRelease(dataProvider);
CGImageRelease(image);
return returnImg;
【问题讨论】:
【参考方案1】:我相信这是解决方案:由于上下文的大小不同并且在原始图像中具有不同的原点,因此您必须偏移在上下文上“绘制”图像的点(对于你们这些更直观的人来说,画布人们)。您首先找到居中上下文的原点,然后将其与所需作物的原点进行比较。可以从上下文的中心点减去差异(或偏移量),这将偏移图像绘制的点。
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);
CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, YES, kCGRenderingIntentDefault);
UIGraphicsBeginImageContext(croppedToRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint contextCenter = CGPointMake(croppedToRect.size.width * 0.5f,
croppedToRect.size.height * 0.5f);
CGPoint centerOfImage = CGPointMake(imageSize.width * 0.5f,
imageSize.height * 0.5f);
CGPoint contextOriginInImage = CGPointMake(centerOfImage.x - contextCenter.x,
centerOfImage.y - contextCenter.y);
CGPoint desiredOrigin = croppedToRect.origin;
CGPoint offset = CGPointMake(desiredOrigin.x - contextOriginInImage.x,
desiredOrigin.y - contextOriginInImage.y);
CGContextTranslateCTM(context, contextCenter.x - offset.x, contextCenter.y - offset.y);
CGContextRotateCTM(context, radians);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextDrawImage(context, CGRectMake(-imageSize.width * 0.5f,
-imageSize.height * 0.5f,
imageSize.width,
imageSize.height), image);
returnImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGDataProviderRelease(dataProvider);
CGImageRelease(image);
【讨论】:
以上是关于如何从文件中旋转和裁剪 iOS CGImage的主要内容,如果未能解决你的问题,请参考以下文章
裁剪和旋转 UIImagePickerController 选择的照片