旋转的图像也应遵循相同的模式,没有空白区域

时间:2012-10-20 06:40:35

标签: objective-c uiimageview uiimage

我正在使用此代码

旋转图像
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{

    CGFloat angleInRadians = angle * (M_PI / 360);

    CGFloat width = CGImageGetWidth(imgRef);

    CGFloat height = CGImageGetHeight(imgRef);


    CGRect imgRect = CGRectMake(0, 0, width, height);

    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);

    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);


    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);

    CGContextSetAllowsAntialiasing(bmContext, YES);

    CGContextSetShouldAntialias(bmContext, YES);

    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);

    CGColorSpaceRelease(colorSpace);

    CGContextTranslateCTM(bmContext,

                          +(rotatedRect.size.width/2),
                          +(rotatedRect.size.height/2));

    CGContextRotateCTM(bmContext, angleInRadians);

    CGContextTranslateCTM(bmContext,
                          -(rotatedRect.size.width/2),
                          -(rotatedRect.size.height/2));

    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                             rotatedRect.size.width,
                                             rotatedRect.size.height),
                       imgRef);




    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);

    CFRelease(bmContext);
   // [(id)rotatedImage autorelease];

    return rotatedImage;


}

通过这个我得到这样的结果图像.. this is i am getting

那些白色空间不应该在那里,它应该遵循那个尺寸的相同图案,而周围的白色空间......旋转的图像....

请帮助..提前谢谢

0 个答案:

没有答案