How can I flip a UIImageView?

后端 未结 6 1109
小蘑菇
小蘑菇 2021-02-09 01:58

How can I flip an UIImageView?

6条回答
  •  温柔的废话
    2021-02-09 02:47

    #import 
    ...
    - (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;
    }  
    

提交回复
热议问题