java 图片旋转保存_旋转图像并以旋转状态保存图像

本文介绍了如何在Java中旋转图像并以旋转后的状态保存。提供了详细的代码示例,使用CGImageRef和Core Graphics进行旋转操作,适用于iOS平台。
摘要由CSDN通过智能技术生成

我找到了问题的解决方案

使用以下方法

- (UIImage*)upsideDownBunny:(CGFloat)radians withImage:(UIImage*)testImage {

__block CGImageRef cgImg;

__block CGSize imgSize;

__block UIImageOrientation orientation;

dispatch_block_t createStartImgBlock = ^(void) {

// UIImages should only be accessed from the main thread

UIImage *img =testImage

imgSize = [img size]; // this size will be pre rotated

orientation = [img imageOrientation];

cgImg = CGImageRetain([img CGImage]); // this data is not rotated

};

if([NSThread isMainThread]) {

createStartImgBlock();

} else {

dispatch_sync(dispatch_get_main_queue(), createStartImgBlock);

}

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

// in iOS4+ you can let the context allocate memory by passing NULL

CGContextRef context = CGBitmapContextCreate( NULL,

imgSize.width,

imgSize.height,

8,

imgSize.width * 4,

colorspace,

kCGImageAlphaPremultipliedLast);

// rotate so the image respects the original UIImage's orientation

switch (orientation) {

case UIImageOrientationDown:

CGContextTranslateCTM(context, imgSize.width, imgSize.height);

CGContextRotateCTM(context, -radians);

break;

case UIImageOrientationLeft:

CGContextTranslateCTM(context, 0.0, imgSize.height);

CGContextRotateCTM(context, 3.0 * -radians / 2.0);

break;

case UIImageOrientationRight:

CGContextTranslateCTM(context,imgSize.width, 0.0);

CGContextRotateCTM(context, -radians / 2.0);

break;

default:

// there are mirrored modes possible

// but they aren't generated by the iPhone's camera

break;

}

// rotate the image upside down

CGContextTranslateCTM(context, +(imgSize.width * 0.5f), +(imgSize.height * 0.5f));

CGContextRotateCTM(context, -radians);

//CGContextDrawImage( context, CGRectMake(0.0, 0.0, imgSize.width, imgSize.height), cgImg );

CGContextDrawImage(context, (CGRect){.origin.x = -imgSize.width* 0.5f , .origin.y = -imgSize.width* 0.5f , .size.width = imgSize.width, .size.height = imgSize.width}, cgImg);

// grab the new rotated image

CGContextFlush(context);

CGImageRef newCgImg = CGBitmapContextCreateImage(context);

__block UIImage *newImage;

dispatch_block_t createRotatedImgBlock = ^(void) {

// UIImages should only be accessed from the main thread

newImage = [UIImage imageWithCGImage:newCgImg];

};

if([NSThread isMainThread]) {

createRotatedImgBlock();

} else {

dispatch_sync(dispatch_get_main_queue(), createRotatedImgBlock);

}

CGColorSpaceRelease(colorspace);

CGImageRelease(newCgImg);

CGContextRelease(context);

return newImage;

}

用这个方法调用

UIImage *rotated2 = [self upsideDownBunny:rotation];

其中rotation是0到360之间的sliderValue .

现在我们可以保存旋转状态 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值