Wednesday, April 6, 2011

UIImage iPhone Rotate 37.8 degrees

After I load an image from the device, I need to rotate it 37.8 degrees then display it on a View.

Is there an function in Objective-C that can do the image rotation?

Ian

From stackoverflow
  • Yes, see my answer to this question: http://stackoverflow.com/questions/1999309/question-about-rotating-a-slider/1999389#1999389

    To convert degrees to radians (for the positionInRadians arg) use this function:

    CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
    
    BahaiResearch.com : I need to rotate just the image though, not the view. Is that possible?
    cidered : Can't you just load the image into a UIImageView and add it as a subview? Otherwise I see KennyTM has answered how to take a UIImage and rotate to get another UIImage.
  • To rotate the view:

    imageView.transform = CGAffineTransformMakeRotation(37.8°);
    

    To rotate the image,

    1. Calculate the width and height will be occupied by the image after rotation.
    2. Create a CGContext by UIGraphicsBeginImageContext.
    3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
    4. [yourImage drawAtPoint:...];
    5. UIGraphicsGetImageFromCurrentImageContext(); and use this image instead.
    6. Release the context.
    Biranchi : Nice Explanation. Thanks

0 comments:

Post a Comment