IOS UIImage Scale and Crop, scaling up actually making image smaller?
我使用别人的捏手势代码进行缩放,效果很好,但是可以在我的照片编辑应用程序中缩放我的图像,一旦用户按下完成缩放,我就需要反映并保存所做的更改,或者说另一种说法如果有人用捏来缩放,我需要将图像实际放大和裁剪。我想我可以使用它们缩放的数量* uigraphicsbeginimagecontext的帧大小,但是该策略不起作用,因为当用户缩放图像并单击完成按钮时,图像会保存得更小,因为现在这非常大的尺寸被挤压到视图中当我真正想要的东西没有任何剩余物而没有做任何拟合时。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | - (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; } |
首先,您能使您的问题更清楚吗?建议您将图像绘制成矩形,而不要挤压图像,对吗?
然后尝试以下方法:
1 2 3 4 | //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 |