How can I flip a UIImageView?

楚嘉纳
2023-12-01

http://stackoverflow.com/questions/1194752/how-can-i-flip-a-uiimageview


#import <QuartzCore/QuartzCore.h>
...
- (UIImage *) flipImageVertically:(UIImage *)originalImage direction:(NSString *)axis {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];

UIGraphicsBeginImageContext(tempImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(0, 0, 0, 0, 0, 0);
if([axis isEqualToString:@"x"])
{
    flipVertical = CGAffineTransformMake(
                                         1, 0, 0, -1, 0, tempImageView.frame.size.height
                                        );
}
else if([axis isEqualToString:@"y"] )
{
    flipVertical = CGAffineTransformMake(
                                         -1, 0, 0, 1, tempImageView.frame.size.width, 0
                                        );
}
CGContextConcatCTM(context, flipVertical);  

[tempImageView.layer renderInContext:context];

UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[tempImageView release];

return flipedImage;
}  



//

imageView 是一个UIImageView的实例
        //左右flip
       imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);

 //上下flip

       imageView.transform = CGAffineTransformMakeScale(1.0,-1.0);   




 类似资料:

相关阅读

相关文章

相关问答