开发者

rotate UIImage and nothing displays

I used the codes below to rotate an UIImage

double angle = -90;
NSData *imageData = [NSData dataWithContentsOfFile:sss];
UIImage *image =[UIImage imageWithData:imageData];
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();
[myUIImageView  setImage:newImage];

but nothing displays on myUIImageView.

If there is anything wrong?

Welcome any comment

Thanks

interdev


If you're going to be rotating the image 90 degrees, you should swap the width and height when you create the image context:

CGSize s = {image.size.height, image.size.width};

This way, there's enough space to draw the fully-transformed image. Attempting to draw an image into a rectangle which exceeds the bounds of a graphics context will just result in nothing being drawn.

P.S. I recommend using a CGBitmapContext instead of UIGraphicsBeginImageContext(), as the former is thread-safe, which means you'd be able to do your image manipulations on a background thread if you so chose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜