【图片旋转】很怪异的问题

程老师2088 程老师2088 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
从相册里面拿到一张照片,如果是自己手机上拍的图片,旋转就会出现问题,如果是外部导入的图片,旋转就正确。。实在不知道怎么回事。。附上旋转的代码。。

- (UIImage *)rotatedImage:(UIImage *)imageToRotate withRotion:(NSInteger)rotation
{

CGFloat rotationAngle = rotation * M_PI / 180;
CGSize imageSize = imageToRotate.size;

// Image size after the transformation
CGSize outputSize = [self sizeForRotatedImage:imageToRotate withRotation:rotation];


CGSize absoluteOutputSize = CGSizeAbsolute(outputSize);


UIImage *outputImage = nil;

// Create the bitmap context
UIGraphicsBeginImageContext(absoluteOutputSize);
CGContextRef imageContextRef = UIGraphicsGetCurrentContext();

// Set the anchor point to {0.5, 0.5}
CGContextTranslateCTM(imageContextRef, .5 * absoluteOutputSize.width, .5 * absoluteOutputSize.height);

// Apply rotation
CGContextRotateCTM(imageContextRef, rotationAngle);

// Draw the current image
CGContextScaleCTM(imageContextRef, 1.0, -1.0);
CGContextDrawImage(imageContextRef, (CGRect) {{-(.5 * imageSize.width), -(.5 * imageSize.height)}, imageSize}, imageToRotate.CGImage);

outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return outputImage;
}

- (CGSize)sizeForRotatedImage:(UIImage *)imageToRotate withRotation:(NSInteger)rotation
{
if (imageToRotate == nil) {
return CGSizeMake(0, 0);
}

CGFloat rotationAngle = rotation * M_PI / 180;

CGSize imageSize = imageToRotate.size;
// Image size after the transformation
CGSize outputSize = CGSizeApplyAffineTransform(imageSize, CGAffineTransformMakeRotation(rotationAngle));

return outputSize;
}
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
程老师2088
iOS 29,038 成员 12,459 内容