View Full Version : how to rotate an image
priyank.ranka
Aug 8, 2008, 07:33 AM
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
Jeremy1026
Aug 8, 2008, 07:53 AM
You will need to import the Core Graphics Framework into your project, then use the following code
nameOfImage.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
That will rotate it 90˚*CW.
priyank.ranka
Aug 11, 2008, 03:19 AM
it gives an error as image itself is an object of UIImage which is been displayed on view.Do you an alternative to this
cmaier
Aug 11, 2008, 10:24 AM
it gives an error as image itself is an object of UIImage which is been displayed on view.Do you an alternative to this
what is the error message? the cited code is the correct technique.
kcjones
Aug 11, 2008, 11:35 AM
I think the
transform = CGAffineTransformMakeRotation
requires the CoreGraphics framework, don't forget to add it otherwise you will get an error.
priyank.ranka
Aug 12, 2008, 02:12 AM
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.
priyank.ranka
Aug 12, 2008, 05:01 AM
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
suhas.gavas
Mar 30, 2010, 10:47 AM
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 :)
satyam90
May 14, 2010, 03:07 AM
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.
- (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);
}
kainjow
May 14, 2010, 09:55 AM
^ 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.
cmaier
May 14, 2010, 10:20 AM
^ 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.
Also it doesn't rotate anything unless you use imageview to display the image.
kainjow
May 14, 2010, 10:22 AM
Also it doesn't rotate anything unless you use imageview to display the image.
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.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.