how to rotate an image

Discussion in 'iOS Programming' started by priyank.ranka, Aug 8, 2008.

  1. priyank.ranka macrumors member

    Joined:
    Jun 11, 2008
    #1
    Hi Everyone,
    I am writing the code to rotate the image which is an object of UIView class,
    but whn i apply transformation it is applied on the entire view object which affects all the images which view object has. Is there any way to rotate only one particular image in the view without affecting other images on the same view..please send your replies as soon as possible

    Best Regards,
    Priyank Ranka
     
  2. Jeremy1026 macrumors 68020

    Jeremy1026

    Joined:
    Nov 3, 2007
    #2
    You will need to import the Core Graphics Framework into your project, then use the following code

    Code:
    nameOfImage.transform = CGAffineTransformMakeRotation(M_PI / 2.0); 
    That will rotate it 90˚*CW.
     
  3. priyank.ranka thread starter macrumors member

    Joined:
    Jun 11, 2008
    #3
    it gives an error as image itself is an object of UIImage which is been displayed on view.Do you an alternative to this
     
  4. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #4
    what is the error message? the cited code is the correct technique.
     
  5. kcjones macrumors newbie

    Joined:
    Aug 4, 2008
    #5
    I think the
    Code:
    transform = CGAffineTransformMakeRotation
    requires the CoreGraphics framework, don't forget to add it otherwise you will get an error.
     
  6. priyank.ranka thread starter macrumors member

    Joined:
    Jun 11, 2008
    #6
    When i apply this technique it is apply to the context and not to the image. And image doesnt have transform as the property. When i apply the same logic it gives me an error "transform is something which is not structured".I want to know what type of Object is an image is.
     
  7. priyank.ranka thread starter macrumors member

    Joined:
    Jun 11, 2008
    #7
    Actually i have the following situation..I have two images (let name them as "A" and "B") say for example. It can be either object of UIImage or CGImageRef. Both belong to same view. Now i want to rotate image "A" which is a background image and i want to display image "B" which is small above the image "A".This is the situation. If I create a separate view for image "A" and rotate it by using CGAffineTransformation it will roatate image "A" but you wont be able to display image "B" over image "A".

    this is the situation in which i am stuck into.Please help me out so that i can rotate one image and display a static image over it
     
  8. suhas.gavas macrumors newbie

    Joined:
    Mar 30, 2010
    #8
    how to rotate an image

    Hi Priyank,
    What you can do is have UIImageview and add it to ur view.
    And thus u can apply rotation to ur UIImageview.
    This will solve ur problem :)
     
  9. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #9
    Hi Suhas, I did the same way but still it didn't solve the problem. The following is the piece of code that I'm using. I'll pass the image as argument to this function.

    Code:
    - (UIImage *)rotateImage:(UIImage *)image {
    	
    	int orient = image.imageOrientation;
    	
    	UIImageView *imageView = [[UIImageView alloc] init];
    	
    	UIImage *imageCopy = [[UIImage alloc] initWithCGImage:image.CGImage];
    	
    	switch ([UIDevice currentDevice].orientation) {
    		case UIImageOrientationLeft:
    			imageView.transform = CGAffineTransformMakeRotation(3 * M_PI / 2.0);
    			break;
    		case UIImageOrientationRight:
    			imageView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
    			break;
    		case UIImageOrientationDown: //EXIF = 3
    			imageView.transform = CGAffineTransformMakeRotation(M_PI);
    		default:
    			break;
    	}
    	
    	imageView.image = imageCopy;
    	return (imageView.image);
    }
    
     
  10. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
    ^ that code has a few issues:

    1. The imageOrientation property's type is UIImageOrientation. You should use that instead of int.
    2. You are leaking imageView
    3. You are leaking imageCopy.
     
  11. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #11
    Also it doesn't rotate anything unless you use imageview to display the image.
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    That is what I thought, but wasn't sure since I didn't test it.

    It'd be better to create a new image and draw the existing image into it with the transform, if you need the rotation saved beyond displaying purposes.
     

Share This Page