我有 UIImage
是 UIImageOrientationUp
(肖像),我想逆时针旋转90度(到风景) . 我不想使用 CGAffineTransform
. 我希望 UIImage
的像素实际上移位 . 我正在使用一段代码(如下所示),最初旨在调整 UIImage
的大小来执行此操作 . 我将目标大小设置为 UIImage
的当前大小,但是我收到错误:
(错误):CGBitmapContextCreate:无效数据字节/行:对于8个整数位/组件,3个组件,kCGImageAlphaPremultipliedLast,至少应为1708 .
(每当我提供SMALLER尺寸作为目标尺寸BTW时,我都不会收到错误) . 如何在保留当前大小的同时仅使用核心图形功能旋转我的 UIImage
90度CCW?
-(UIImage*)reverseImageByScalingToSize:(CGSize)targetSize:(UIImage*)anImage
{
UIImage* sourceImage = anImage;
CGFloat targetWidth = targetSize.height;
CGFloat targetHeight = targetSize.width;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
19 回答
这看起来很奇怪,下面的代码为我解决了这个问题:
怎么样的:
我相信最简单的方法(也是线程安全)是:
注意:正如 Brainware 所说,这只会修改图像的方向数据 - 像素数据不受影响 . 对于某些应用程序,这可能还不够 .
或者在Swift中:
查看Hardy Macia简单而精彩的代码:cutting-scaling-and-rotating-uiimages
打电话吧
谢谢Hardy Macia!
Headers :
*(UIImage )imageAtRect:(CGRect)rect;
*(UIImage )imageByScalingProportionallyToMinimumSize:(CGSize)targetSize;
*(UIImage )imageByScalingProportionallyToSize:(CGSize)targetSize;
*(UIImage )imageByScalingToSize:(CGSize)targetSize;
*(UIImage )imageRotatedByRadians:(CGFloat)radians;
*(UIImage )imageRotatedByDegrees:(CGFloat)degrees;
由于链接可能会死亡,这里是完整的代码
线程安全旋转功能如下(它工作得更好):
我遇到了上述问题,包括批准的答案 . 我将Hardy的类别转换回方法,因为我想要的只是旋转图像 . 这是代码和用法:
用法:
谢谢哈迪!
Rotate Image by 90 degree (clockwise/anti-clockwise direction)
功能调用 -
执行:
如果你想添加一个照片旋转按钮,它将继续以90度的增量旋转照片,你走了 . (
finalImage
是已在其他地方创建的UIImage . )简单 . 只需更改图像方向标志即可 .
Swift 3 UIImage扩展:
这是UIImage的Swift扩展,可以将图像旋转任意角度 . 像这样使用它:
let rotatedImage = image.rotated(byDegrees: degree)
. 我在其中一个答案中使用了Objective-C代码并删除了一些我们不正确的行(旋转的框内容)并将其转换为UIImage的扩展 .我尝试这个代码,它工作,并从http://www.catamount.com/blog/1015/uiimage-extensions-for-cutting-scaling-and-rotating-uiimages/
对基于Hardy Macia代码的其他答案进行微小更改 . 只需计算旋转图像的边界矩形就不需要创建整个
UIView
对象 . 只需使用CGRectApplyAffineTransform
将旋转变换应用于图像矩形 .“tint uiimage灰度”似乎是适合这个的Google-Fu
我马上得到:
https://discussions.apple.com/message/8104516?messageID=8104516�
https://discussions.apple.com/thread/2751445?start=0&tstart=0
How would I tint an image programmatically on the iPhone?
我喜欢简单优雅的
Peter Sarnowski
's answer, but it can cause problems when you can' t依赖于EXIF
元数据之类的东西 . 在你需要 rotate the actual image data 的情况下,我会推荐这样的东西:上面的代码采用方向为
Landscape
(可以't remember if it' sLandscape Left
或Landscape Right
)的图像并将其旋转为Portrait
. 这是一个可以根据您的需求进行修改的示例 .您必须使用的关键参数是
CGContextRotateCTM(context, M_PI_2)
,您可以在其中决定要旋转多少,但是您必须确保图片仍然使用CGContextTranslateCTM(context, 0, -640)
在屏幕上绘制 . 最后一部分非常重要,以确保您看到图像而不是空白屏幕 .有关更多信息,请查看source .
resize-a-uiimage-the-right-way解释了许多代码示例所涉及的一些问题,并且有一些代码片段来帮助处理UIImages - UIImage中的私有帮助器方法resize.m接受转换以允许旋转,所以你只需要公开作为公共界面 .
这是该文件的许可证:
有一个非常有效的UIImage类别,名为NYXImagesKit . 它使用vDSP,CoreImage和vImage尽可能快 . 它有一个UIImage旋转类别,可以节省我的一天:)
https://github.com/Nyx0uf/NYXImagesKit
对于Swift:这是UIImage的简单扩展:
(Source)
使用它:
该如果将flip设置为true,则前者将旋转图像并翻转它 .
Swift 4.2版RawMean's answer: