UIImage 意外调整大小

Posted

技术标签:

【中文标题】UIImage 意外调整大小【英文标题】:UIImage resizes unexpectedly 【发布时间】:2016-04-05 09:37:17 【问题描述】:

我在视图控制器中有视图控制器我已经放置了一个 UIImageView 我试图以这种方式在 UIImageView 中的图像上绘制。

- (void)viewDidLoad 
 [super viewDidLoad];
 [self.drawImageView setImage:editableImage];
 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingViewDidPan:)];
 panGesture.maximumNumberOfTouches = 1;
 self.drawImageView.userInteractionEnabled = YES;
 [self.drawImageView addGestureRecognizer:panGesture];

其中 drawImageView 是我的 UIImageView,editableImage 是我要绘制的 UIImage。

我正在实现绘图部分如下

- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender

  CGPoint currentDraggingPosition = [sender locationInView:drawImageView];

  if(sender.state == UIGestureRecognizerStateBegan)
     prevDraggingPosition = currentDraggingPosition;
  

  if(sender.state != UIGestureRecognizerStateEnded)
      [self drawLine:prevDraggingPosition to:currentDraggingPosition];
  
  prevDraggingPosition = currentDraggingPosition;


-(void)drawLine:(CGPoint)from to:(CGPoint)to

  CGSize size = drawImageView.frame.size;
  UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

  CGContextRef context = UIGraphicsGetCurrentContext();

  [drawImageView.image drawAtPoint:CGPointZero];

  CGFloat strokeWidth = 5.0;
  UIColor *strokeColor = [UIColor redColor];

  CGContextSetLineWidth(context, strokeWidth);
  CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
  CGContextSetLineCap(context, kCGLineCapRound);


  CGContextMoveToPoint(context, from.x, from.y);
  CGContextAddLineToPoint(context, to.x, to.y);
  CGContextStrokePath(context);

  drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

问题是,只要我用手指绘制 UIImage 就会调整大小并变小,其余一切正常

我该如何解决这个问题?

我的 editableImage 来自另一个视图控制器,它有一个名为 draw 的按钮

-(IBAction)drawOnOmage:(id)sender 
    [self performSegueWithIdentifier:@"MySegue1" sender:self];


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
  ThirdViewController *destinationController = (ThirdViewController *)segue.destinationViewController;
  destinationController->editableImage = self.editImage.image;

【问题讨论】:

编辑你的问题,告诉我们editableImage来自哪里! 我刚做了,但是一旦我触摸它调整大小的图像,出现的图像就很好了 【参考方案1】:

我刚刚将您的代码复制到一个新项目中,它似乎对我有用。我唯一注意到的是,您没有为prevDraggingPosition 使用@property 声明-也许这就是问题所在?你能告诉我们你是如何声明prevDraggingPosition的吗?

看看我的例子(对我有用,我只添加了self.):

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) IBOutlet UIImageView *drawImageView;
@property (nonatomic, assign) CGPoint prevDraggingPosition;

@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingViewDidPan:)];
    panGesture.maximumNumberOfTouches = 1;
    self.drawImageView.userInteractionEnabled = YES;
    [self.drawImageView addGestureRecognizer:panGesture];



- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender

    CGPoint currentDraggingPosition = [sender locationInView:self.drawImageView];

    if(sender.state == UIGestureRecognizerStateBegan)
        self.prevDraggingPosition = currentDraggingPosition;
    

    if(sender.state != UIGestureRecognizerStateEnded)
        [self drawLine:self.prevDraggingPosition to:currentDraggingPosition];
    
    self.prevDraggingPosition = currentDraggingPosition;


-(void)drawLine:(CGPoint)from to:(CGPoint)to

    CGSize size = self.drawImageView.frame.size;
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

    CGContextRef context = UIGraphicsGetCurrentContext();

    [self.drawImageView.image drawAtPoint:CGPointZero];

    CGFloat strokeWidth = 5.0;
    UIColor *strokeColor = [UIColor redColor];

    CGContextSetLineWidth(context, strokeWidth);
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
    CGContextSetLineCap(context, kCGLineCapRound);


    CGContextMoveToPoint(context, from.x, from.y);
    CGContextAddLineToPoint(context, to.x, to.y);
    CGContextStrokePath(context);

    self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


@end

【讨论】:

我在接口文件中添加了 prevDraggingPosition 作为公共变量。 如果你把它设为@property,就像我在上面的例子中展示的那样,它不起作用? 编辑您的问题并添加您如何声明prevDraggingPositiondrawImageView 我的图片来自另一个视图控制器会导致问题

以上是关于UIImage 意外调整大小的主要内容,如果未能解决你的问题,请参考以下文章

调整 UIImage 大小的哪种方法更好,为啥?

调整 UIImage 大小 - 性能问题

UIImage 未根据 UIImageView 大小宽度和高度调整大小

按宽度按比例调整 UIImage 的大小

UIImage 调整大小

通过保持纵横比和宽度调整 UIImage 的大小