Zhang Horatio
Zhang Horatio

Reputation: 11

Why imageWithCGImage:scale:orientation: doesn't work?

This is my question:

I want to rotate my image when I got the degree it should rotate.

And my code is here

UIImage *image = imageView.image;
UIImage *originalImage = imageView.image;
CGAffineTransform transform = imageView.transform;

        if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2))) {
            image = [UIImage imageWithCGImage:originalImage.CGImage scale:originalImage.scale orientation:UIImageOrientationRight];
        } else if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI))) {
            image = [UIImage imageWithCGImage:originalImage.CGImage scale:originalImage.scale orientation:UIImageOrientationDown];
        } else if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2 * 3))) {
            image = [UIImage imageWithCGImage:originalImage.CGImage scale:originalImage.scale orientation:UIImageOrientationLeft];
        } else if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * 2))) {
            image = originalImage; // UIImageOrientationUp
        }

As I hope , the image will show as rotated. But after rotating, this images is still what it was. It means the method imageWithCGImage:scale:orientation: didn't work.

Some one can tell me why? Thanks.

Upvotes: 0

Views: 2361

Answers (3)

borrrden
borrrden

Reputation: 33421

This is something that you will need to fundamentally understand. When you get a pointer to another object, and then set that pointer to something else, it doesn't affect the original object. Think of it this way:

Lunch *myLunch = myFriend.CurrentLunch;

This means that your lunch is current your friend's lunch.

myLunch = new MisoRamen();

Now your lunch is a different lunch.

myLunch.InsertWeirdSauce();

You just inserted a sauce into your own lunch, while your friend's lunch remains safe. If you want to change your friend's lunch you have to do this.

Lunch *newLunch = new MisoRamen();
newLunch.InsertWeirdSauce();
myFriend.CurrentLunch = newLunch;

Now your friend will gasp in shock as he/she eats a lunch with weird sauce in it.

Upvotes: 0

Nathan Ou
Nathan Ou

Reputation: 307

You use

UIImage *image = imageView.image;

to get the handle of imageView.image, and you've set your changed image for (UIImage*)image which is just a reference of imageView.image.So if you want imageView.image to be change, you should set imageView.image again to make it change

add this in the back after your codes

imageView.image = image;

Upvotes: 1

Khanh Nguyen
Khanh Nguyen

Reputation: 11132

You need set the image again (after all that code)

imageView.image = image;

Upvotes: 0

Related Questions