-2

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

- (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];
}

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

**

编辑:

**

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

修改方法有:

- (void)drawRect:(CGRect)rect
{
    NSLog(@"drawRect");
    // 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);
    CGContextDrawImage(context, imageRect, [self rotateImage: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;
        lastRotation = gestureRecognizer.rotation;       
         [gestureRecognizer setRotation:0];

    }
    [self setNeedsDisplay];
}
- (UIImage *)rotateImage:(UIImage *) img
{
    NSLog(@"rotateImage");

    CGAffineTransform transform = imageView.transform;
    transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height);
    transform = CGAffineTransformRotate(transform, lastRotation);
    transform = CGAffineTransformScale(transform, -1, -1);

    CGContextRef ctx = CGBitmapContextCreate(NULL, img.size.width, img.size.height,
                                             CGImageGetBitsPerComponent(img.CGImage), 0,
                                             CGImageGetColorSpace(img.CGImage),
                                             CGImageGetBitmapInfo(img.CGImage));
    CGContextConcatCTM(ctx, transform);
    CGContextDrawImage(ctx, CGRectMake(20,20,img.size.width,img.size.height), img.CGImage);

    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return newImg;


}
4

2 回答 2

1

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

查看Quartz 2D 编程指南

于 2013-05-02T09:36:09.507 回答
1

好的。最后我使用ANImageBitmapRep得到了它。我在课堂上声明了 lastRotation, angle,rotateMe RotatedView.h

    -(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);

 }

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

于 2013-05-04T05:19:32.603 回答