UIImageView 旋转而不移动图像 [英] UIImageView rotation without moving Image

查看:29
本文介绍了UIImageView 旋转而不移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想旋转我的 UIImageView 而不移动整个png".没有代码只是为了测试会发生什么_fanImage.transform = CGAffineTransformMakeRotation(45);

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 ?

推荐答案

您可以尝试这样的操作.您应该旋转 UIImage 而不是 UIImageView.

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;
}

这篇关于UIImageView 旋转而不移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆