3
votes

I have a problem in scaling the uiimageview which is placed inside the uiscrollview. I have googled and checked all the questions related to my problem in StackOverflow as well. I tried all the answers that are posted in the StackOverflow also. Nothing worked for me.

First I am placing the uiimageview inside uiscrollview in nib file and I am taking the image from Camera roll and filling the image view. Then I am using uirotationgesturerecognizer to rotate the image.

Here is the code that I am trying to do.

- (void)viewDidLoad
{
 [super viewDidLoad];
 NSLog(@"%@",[[UIDevice currentDevice] model]);

// Do any additional setup after loading the view, typically from a nib.
 self.imagePicker = [[[UIImagePickerController alloc] init] autorelease];

 self.picChosenImageView.layer.shouldRasterize = YES;
 self.picChosenImageView.layer.rasterizationScale = [UIScreen mainScreen].scale;

 self.picChosenImageView.layer.contents = (id)[UIImage imageNamed:@"test"].CGImage;
 self.picChosenImageView.layer.shadowColor = [UIColor blackColor].CGColor;
 self.picChosenImageView.layer.shadowOpacity = 0.8f;
 self.picChosenImageView.layer.shadowRadius = 8;
 self.picChosenImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.picChosenImageView.bounds].CGPath;

 UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc]initWithTarget:self
                                                                                               action:@selector(handleRotate:)] autorelease];
 rotationRecognizer.delegate = self;
 [self.picChosenImageView addGestureRecognizer:rotationRecognizer];

 self.containerView.delegate = self;
 self.containerView.contentSize = self.picChosenImageView.layer.frame.size;
 self.containerView.maximumZoomScale = 4.0f;
 self.containerView.minimumZoomScale = 1.0f;
 angle = 0.0f;
 useRotation = 0.0;
 isRotationStarted=FALSE;
 isZoomingStarted = FALSE; 
}

-(void)lockZoom
{
 maximumZoomScale = self.containerView.maximumZoomScale;
 minimumZoomScale = self.containerView.minimumZoomScale;

 self.containerView.maximumZoomScale = 1.0;
 self.containerView.minimumZoomScale = 1.0;
 self.containerView.clipsToBounds = false;
 self.containerView.scrollEnabled = false;
}

-(void)unlockZoom
{
    self.containerView.maximumZoomScale = maximumZoomScale;
    self.containerView.minimumZoomScale = minimumZoomScale;
    self.containerView.clipsToBounds = true;
    self.containerView.scrollEnabled = true;
}

#pragma mark - ScrollView delegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return  self.picChosenImageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

    CGRect frame = self.picChosenImageView.frame;
    frame.origin = CGPointZero;
    self.picChosenImageView.frame = frame;
    //self.picChosenImageView.transform = prevTransform;

}

-(void) scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    if(!isZoomingStarted)
    {
        self.picChosenImageView.transform = CGAffineTransformRotate(self.picChosenImageView.transform, angle);
        NSLog(@"The zooming started");
        isZoomingStarted = TRUE;
        CGSize contentSize = self.containerView.bounds.size;
        CGRect contentFrame = self.containerView.bounds;
        NSLog(@"frame on start: %@", NSStringFromCGRect(contentFrame));
        NSLog(@"size on start: %@", NSStringFromCGSize(contentSize));
        //prevTransform = self.picChosenImageView.transform;
    }
}

-(void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    if(isZoomingStarted)
    {
        self.picChosenImageView.transform = CGAffineTransformRotate(self.picChosenImageView.transform, angle);
        isZoomingStarted = FALSE;
        CGSize contentSize = self.containerView.contentSize;
        CGRect contentFrame = self.containerView.bounds;
        NSLog(@"frame on end: %@", NSStringFromCGRect(contentFrame));
        NSLog(@"size on end: %@", NSStringFromCGSize(contentSize));
    }
}

#pragma mark - GestureRecognizer methods

- (void) handleRotate:(UIRotationGestureRecognizer *)recognizer
{
    if(isZoomingStarted == FALSE)
    {
        if([recognizer state] == UIGestureRecognizerStateBegan)
        {
            angle = 0.0f;
            [self lockZoom];
        }

        useRotation+= recognizer.rotation;

        while( useRotation < -M_PI )
        {
            useRotation += M_PI*2;
        }
        while( useRotation > M_PI )
        {
            useRotation -= M_PI*2;
        }
        NSLog(@"The rotated value is %f",RADIANS_TO_DEGREES(useRotation));
        self.picChosenImageView.transform = CGAffineTransformRotate([self.picChosenImageView transform],
                                                                    recognizer.rotation);
        [recognizer setRotation:0];

        if([recognizer state] == UIGestureRecognizerStateEnded)
        {
            angle = useRotation;
            useRotation = 0.0f;
            isRotationStarted = FALSE;
            self.containerView.hidden = NO;
            //prevTransform = self.picChosenImageView.transform;
            [self unlockZoom];
        }
    }
}

My problem is, I am able to successfully do a zoom in and zoom out. I am able to rotate the uiimageview as I wanted to. After rotating the uiimageview to a certain angle, and when I am trying to zoom in, the imageview gets back to the original position (rotate itself back to zero degree) and then the zooming happens. I want to retain the rotation and also zoom. I tried saving the previous transform and assign in back scrollDidzoom and scrollDidBegin delegate methods. None worked. Please help me to spot my mistake which I am overlooking.

2
Adding the below line of code to scrollViewWillBeginZooming and endZooming, makes the imageview to get back to the original rotation self.picChosenImageView.transform = CGAffineTransformRotate(self.picChosenImageView.transform, angle);jeevangs
But I am not able to have the rotation of the imageview retained, while the zooming is happening, I mean while scrollviewdidZoomjeevangs
I'm facing the same problem.. The problem is the setZoomScale-Method. It's overriding the applied rotation. See stackoverflow.com/questions/12006171/…user289841

2 Answers

0
votes

try using CGAffineTransformScale instead of just resizing the frame for zooming:

anImage.transform = CGAffineTransformScale(anImage.transform, 2.0, 2.0);

changing the transform for scaling might fix your rotation issue.

hope this helps.

0
votes

I had the same problem. UIScrollView is taking control over UIImageView and it is using transform without rotation.

So I do not give image reference to scroll and I have added UIPinchGestureRecognizer for scaling.

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
  return nil
}

Dragging is still working :)

// viewDidLoad
var pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(pinchRecogniezed))
scrollView.addGestureRecognizer(pinchGestureRecognizer)


func pinchRecogniezed(sender: UIPinchGestureRecognizer) {
    if sender.state == .Began || sender.state == .Changed {
        let scale = sender.scale
        imageView.transform = CGAffineTransformScale(imageView.transform, scale, scale)
        sender.scale = 1
    }
}