如何在uiview中获取imageview的旋转图像?

时间:2013-04-29 10:00:50

标签: ios uiimageview uigesturerecognizer drawrect

我创建了RotatedView的{​​{1}}子类,并添加了UIView作为UIImageView的子视图,并使用RotatedView方法在RotatedView上绘制了图片与imageView的图像相同。我在imageView上应用了捏合和旋转手势。当我捏住imageView绘图图像时也会改变。但是当我旋转imageView时,绘图图像不会改变。我使用了以下代码::

drawRect:

当旋转imageView时,如何在UIView中获得旋转图像?

**

编辑:

**

我使用第一种方法得到解决方案。现在我正在使用第二种方法。我认为这很简单也是最好的,但我不确定哪一个是最好的。在第二种方法中,图像旋转但不在中心点。我很难解决这个问题。请帮帮我。

修改方法是:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor redColor];
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 150, 100, 150)];
        imageView.image = [UIImage imageNamed:@"Images_6.jpg"];
        imageView.userInteractionEnabled = YES;
        [self addSubview:imageView];

        UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationMethod:)];
        rotationGesture.delegate = self;
        [imageView addGestureRecognizer:rotationGesture];



    }
    return self;
}



- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGRect rectFrame = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
     CGContextSetLineWidth(context, 10.0);
    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 90, 0);
    CGContextAddLineToPoint(context, 90, 90);

    CGContextAddLineToPoint(context, 0, 90);

    CGContextAddLineToPoint(context, 0, 0);



    CGContextClip(context);


    CGContextStrokePath(context);
    CGContextDrawImage(context,rectFrame, imageView.image.CGImage);
}

-(void)rotationMethod:(UIRotationGestureRecognizer *)gestureRecognizer{
    NSLog(@"rotationMethod");
    if ([gestureRecognizer state]==UIGestureRecognizerStateBegan || [gestureRecognizer state]==UIGestureRecognizerStateChanged) {
        CGAffineTransform transform = CGAffineTransformRotate(gestureRecognizer.view.transform, gestureRecognizer.rotation);
        gestureRecognizer.view.transform = transform;
         [gestureRecognizer setRotation:0];

    }
    [self setNeedsDisplay];
}

2 个答案:

答案 0 :(得分:1)

您需要使用CGContextScaleCTMCGContextRotateCTM(并可能适当地转换您的CGContextRef以匹配您的UIImageView转换。

查看Quartz 2D Programming Guide

答案 1 :(得分:1)

确定。最后我用ANImageBitmapRep得到它。我在RotatedView.h类声明了lastRotation,angle,rotateMe。

    -(void)rotationMethod:(UIRotationGestureRecognizer *)gestureRecognizer{

           if ([gestureRecognizer state]==UIGestureRecognizerStateBegan) {
               if (!rotateMe) {
                    rotateMe = [[ANImageBitmapRep alloc] initWithImage:displayImageView.image];
               }else{
                    rotateMe =[ANImageBitmapRep imageBitmapRepWithImage:displayImageView.image];
               }



        }
        CGFloat rotation = 0.0 - (lastRotation - [gestureRecognizer rotation]);

        CGAffineTransform currentTransform = [gestureRecognizer view].transform;
        CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

        [[gestureRecognizer view] setTransform:newTransform];

        lastRotation = [gestureRecognizer rotation];
        angle =lastRotation * (180 / M_PI);

        [self setNeedsDisplay];
    }

 - (void)drawRect:(CGRect)rect
  {
            CGRect imageRect = CGRectMake(0, 0, 90, 90);
            // Drawing code
            CGContextRef context =UIGraphicsGetCurrentContext();
            CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
             CGContextSetLineWidth(context, 10.0);
            CGContextBeginPath(context);

            CGContextMoveToPoint(context, imageRect.origin.x, imageRect.origin.y);
            CGContextAddLineToPoint(context, imageRect.size.width, imageRect.origin.y);
            CGContextAddLineToPoint(context, imageRect.size.width, imageRect.size.height);

            CGContextAddLineToPoint(context, imageRect.origin.x, imageRect.size.height);

            CGContextAddLineToPoint(context, imageRect.origin.x, imageRect.origin.y);

            CGContextClip(context);


            CGContextStrokePath(context);

            UIImage  *rotatedImage1=[UIImage imageWithCGImage:[rotateMe imageByRotating:angle]];
            CGContextDrawImage(context, imageRect, rotatedImage1.CGImage);

 }

我找到了这个解决方案。如果有人找到最佳解决方案,请根据此问题向我提出建议。