Question

im using someone else's pinch gesture code for scaling which works perfect but its scaling my image in my photo editing app and once the user presses done scaling, i need the changes to be reflected and saved or another way to say it i need the image to actually be zoomed in and cropped if someone used pinch to scale. I figured i could use the amount they scaled * the frame size for uigraphicsbeginimagecontext but that strategy is not working since when the user scales the image and hits the done button the image gets saved smaller because this now very large size is getting squeezed into the view when what i really want it crop off any leftovers and not do any fitting.

- (IBAction)pinchGest:(UIPinchGestureRecognizer *)sender{


if (sender.state == UIGestureRecognizerStateEnded
    || sender.state == UIGestureRecognizerStateChanged) {
    NSLog(@"sender.scale = %f", sender.scale);

    CGFloat currentScale = self.activeImageView.frame.size.width / self.activeImageView.bounds.size.width;
    CGFloat newScale = currentScale * sender.scale;

    if (newScale < .5) {
        newScale = .5;
    }
    if (newScale > 4) {
        newScale = 4;
    }

    CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
    self.activeImageView.transform = transform;
    scalersOfficialChange = newScale;
    sender.scale = 1;

}
}

- (IBAction)doneMoverViewButtonPressed:(UIButton *)sender {
// turn off ability to move & scale
moverViewActive = NO;

NSLog(@"%f %f",dragOfficialChange.x,dragOfficialChange.y);
NSLog(@"%f",rotationOfficialChange);
NSLog(@"%f",scalersOfficialChange);


//problem area below...
CGSize newSize = CGSizeMake(self.activeImageView.bounds.size.width * scalersOfficialChange, self.activeImageView.bounds.size.height * scalersOfficialChange );

UIGraphicsBeginImageContext(newSize);

[self.activeImageView.image drawInRect:CGRectMake(dragOfficialChange.x, dragOfficialChange.y, self.layerContainerView.bounds.size.width, self.layerContainerView.bounds.size.height)];

self.activeImageView.image = UIGraphicsGetImageFromCurrentImageContext();


UIGraphicsEndImageContext();

[self hideMoveViewerAnimation];

//resets activeimageview coords
CGRect myFrame = self.layerContainerView.bounds;
myFrame.origin.x = 0;
myFrame.origin.y = 0;
self.activeImageView.frame = myFrame;

//reset changes values
dragOfficialChange.x = 0;
dragOfficialChange.y = 0;
rotationOfficialChange = 0;
scalersOfficialChange = 0;

}
Was it helpful?

Solution

first of all, can you make your question more clear? I suggest you want to draw your image in a rect and don't want to squeeze it, am I right?

Then lets try this method:

//The method: drawInRect:(CGRect) will scale your pic and squeeze it to fit the Rect area
//So if you dont want to scale your pic, you can use the method below
[image drawAsPatternInRect:(CGRect)_rect];
//This method would not scale your image and cut the needless part
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top