【问题标题】:Image not rotating correctly图像未正确旋转
【发布时间】:2012-11-13 23:20:28
【问题描述】:

rotate我正在尝试将我的图像旋转 90 度。它在没有旋转的情况下工作,但是当我旋转和平移时它不显示。我做错了什么?

            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"check" ofType:@"jpg"];
UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:filePath];

UIGraphicsBeginImageContext(newImage.size);
//CGContextRef c = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), newImage.size.width, 0);
//CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, newImage.size.width);
//CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
CGContextRotateCTM(UIGraphicsGetCurrentContext(), 90);
CGRect imageRect = CGRectMake(0, 0, newImage.size.height, newImage.size.width);   
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, newImage.CGImage);
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

ImageView.image = viewImage;

【问题讨论】:

标签: iphone ios image cocoa


【解决方案1】:

我建议使用图像视图并像这样使用它的变换:

UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *newImageView = [[UIImageView alloc] initWithImage:newImage]; 
newImageView.transform = CGAffineTransformMakeRotation(M_PI_2); 

【讨论】:

  • 我无法使用图像视图,因为该图像必须发送到服务器进行图像授权。
  • 您始终可以使用newImageView.image直接从imageview访问图像
【解决方案2】:

你应该这样做:

UIGraphicsBeginImageContext(newImage.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM (context, 90* M_PI/180); //Degrees should be in radians.

[newImage drawAtPoint:CGPointMake(0, 0)];

UIImage *viewImage =  UIGraphicsGetImageFromCurrentImageContext();

祝你好运。

更新:

其实这是How to Rotate a UIImage 90 degrees?的复制品

【讨论】:

    【解决方案3】:

    问题是我的定义语句,我需要翻转图像。这是完整的代码:(图像必须是完美的,因为 OCR 会处理图像,即使有一点偏差,也会被拒绝。

        CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSaveGState(c);
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"check" ofType:@"jpg"];
    UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:filePath];
    
    NSLog(@"Rect width: %@", NSStringFromCGSize(newImage.size) );
    UIGraphicsBeginImageContext(newImage.size);
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, newImage.size.width);
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
    
    CGContextTranslateCTM( UIGraphicsGetCurrentContext(), 0.5f * newImage.size.width, 0.5f * newImage.size.height ) ;
    CGContextRotateCTM( UIGraphicsGetCurrentContext(), radians( -90 ) ) ;
        CGRect imageRect = CGRectMake(-(newImage.size.height* 0.5f) + 200,  -(0.5f * newImage.size.width) + 200 , newImage.size.height - 200, newImage.size.width - 200);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, newImage.CGImage);
    
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    ImageView.image = viewImage;
    
    CGContextRestoreGState(c);
    

    【讨论】:

      猜你喜欢
      • 2020-12-31
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多