旋转UIImage自定义度 [英] Rotate UIImage custom degree

查看:39
本文介绍了旋转UIImage自定义度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以自定义角度旋转UIImage(而不是UIImageView)我关注了这篇文章,但它对我不起作用.

I want to rotate an UIImage (not UIImageView) in custom degree I followed this post but it didn't work for me.

任何人都可以帮忙吗?谢谢.

Anyone can help? Thanks.

更新:下面的代码完成了一些工作,但是旋转后我丢失了一些图像:

UPDATE: The code below does some of the job, but I lose some of the image after rotating it:

我该怎么做才能正确处理?(顺便说一下,屏幕截图中的黄色是我的UIImageView bg)

What should I change to get it right? (btw the yellow color in the screenshots is my UIImageView bg)

- (UIImage *) rotate: (UIImage *) image
{
    double angle = 20;
    CGSize s = {image.size.width, image.size.height};
    UIGraphicsBeginImageContext(s);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0,image.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    CGContextRotateCTM(ctx, 2*M_PI*angle/360);
    CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

推荐答案

此方法以旋转角度返回图像

This method return you image on your angle of rotate

#pragma mark -
#pragma mark Rotate Image 

- (UIImage *)scaleAndRotateImage:(UIImage *)image  { 

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);

    CGFloat boundHeight;

    boundHeight = bounds.size.height;  
    bounds.size.height = bounds.size.width; 
    bounds.size.width = boundHeight;
    transform = CGAffineTransformMakeScale(-1.0, 1.0);
    transform = CGAffineTransformRotate(transform, M_PI / 2.0); //use angle/360 *MPI

    UIGraphicsBeginImageContext(bounds.size);   
    CGContextRef context = UIGraphicsGetCurrentContext();   
    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;   

}

这篇关于旋转UIImage自定义度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆