Question

Hi,
I want to rotate my UIImageView without moving the whole "png". No code is only to test what happens _fanImage.transform = CGAffineTransformMakeRotation(45);

It turns but the whole image moves. What can I do that this doesn't happen ?

Was it helpful?

Solution

You can try something like this.. You should rotate the UIImage rather than UIImageView.

- (UIImage *)imageWithTransform:(CGAffineTransform)transform {


    CGRect rect = CGRectMake(0, 0, self.size.height, self.size.width);
    CGImageRef imageRef = self.CGImage;

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                self.size.width,
                                                self.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                CGImageGetBitmapInfo(imageRef));

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);



    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, rect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}

OTHER TIPS

I think you mean that you want your image view to rotate around it's center point. Is that right? If so, that's what a view should do by default.

You should do a search on "Translating, Scaling, and Rotating Views" in Xcode and read the resulting article.

Note that all of iOS's angles are specified in radians, not degrees.

Your sample images aren't really helpful, since we can't see the frame that the image view is drawn into. It's almost impossible to tell what your image views are doing and what they are supposed to be doing instead based on the pictures you linked from your dropbox.

A full 360 degrees is 2pi.

You should use

CGFloat degrees = 45;
CGFloat radians = degrees/180*M_PI;
_fanImage.transform = CGAffineTransformMakeRotation(radians);

That will fix the rotation amount for your code, but probably not the rotation position.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top