2011-11-20 120 views
0

我想在90度用此方法旋轉的UIImage:旋轉UIImage的給予空白的UIImage

static inline double radians (double degrees) {return degrees * M_PI/180;} 
UIImage* rotate(UIImage* src, UIImageOrientation orientation) 
{ 
    UIGraphicsBeginImageContext(src.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orientation == UIImageOrientationRight) { 
     CGContextRotateCTM (context, radians(90)); 
    } else if (orientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (context, radians(-90)); 
    } else if (orientation == UIImageOrientationDown) { 
     // NOTHING 
    } else if (orientation == UIImageOrientationUp) { 
     CGContextRotateCTM (context, radians(90)); 
    } 

    [src drawAtPoint:CGPointMake(0, 0)]; 

    return UIGraphicsGetImageFromCurrentImageContext(); 
} 

的問題是,它給我一個空白的UIImage

回答

1

這裏是代碼: -

UIImage *PortraitImage; 
PortraitImage = [[UIImage alloc] initWithCGImage: currentImage.CGImage 
                scale: 1.0 
               orientation: UIImageOrientationLeft]; 

如果你想旋轉UIImage在某個特定的角度,那麼你可以使用CIIFilter旋轉任何角度,但這是可能的IOS 5.0

CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"old-country-rain.jpg"]]; 
CIFilter *RotateFilter = [CIFilter filterWithName:@"CIStraightenFilter"]; 
[RotateFilter setValue:[NSNumber numberWithFloat: 1.04667f] forKey:@"inputAngle"]; 
[RotateFilter setValue:inputImage forKey:kCIInputImageKey]; 
CIImage *displayImage = RotateFilter.outputImage; 
UIImage *finalImage = [UIImage imageWithCIImage:displayImage]; 
+0

我發現CIImage範圍在旋轉後不會改變。任何建議?我的圖像的displayImage.extents是640 x 480.旋轉後,displayImage.extents仍然是640 x 480。 – Christopher