簡體   English   中英

UIImageView基於UIView掩碼的圖像裁剪

[英]UIImageView image crop based on UIView mask

所以我有一個畫布(UIView)和一個UIImageView,畫布充當了imageview上的一個遮罩

在此輸入圖像描述

我正在使用UIGestureRecognizers來縮放和旋轉畫布下的UIImageView。

我想轉換最終圖像(在畫布中顯示為UIImage,一種解決方案是將畫布轉換為如下圖像

UIGraphicsBeginImageContext(self.canvas.bounds.size);
[self.canvas.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newCombinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

現在這個工作正常,但這個解決方案的問題是圖像被裁剪為畫布的尺寸,因此分辨率非常低。

我探索的另一個選擇是使用一些自定義UIImage類別來旋轉和縮放。

[[[self.photoImage image] imageRotatedByDegrees:rotaton_angle] 
     imageAtRect:CGRectMake(x,y width,height)]

我需要提供旋轉角度(UIGesture Delegate提供的旋轉角度不是度數或弧度,然后有x,y,寬度,高度,我想這些需要根據某種尺度計算,(我確實得到了比例值)來自UIGesture代表,但他們似乎沒有正確的這個功能)

這里有很多解決方案,可以指導你給出一個矩形的裁剪和圖像。 但在我的情況下,矩形與圖像的尺度不同,也涉及旋轉。

任何幫助將不勝感激。

我設法解決了這個問題,這是我的解決方案,它絕對不是最干凈的,但它確實有效。

我需要處理3件事,平移,縮放和旋轉。

首先,我使用UIGestureRecognizer委托來獲取所有3的UIGestureRecognizerStateEnded累積值。

然后為了輪換我剛剛使用了這里討論的UIImage類別

    self.imagetoEdit = [self.imagetoEdit imageRotatedByRadians:total_rotation];

縮放(縮放)我使用GPUImage(我在整個應用程序中使用此功能)

GPUImageTransformFilter *scaleFilter = [[GPUImageTransformFilter alloc] init];
[scaleFilter setAffineTransform:CGAffineTransformMakeScale(total_scale, total_scale)];
[scaleFilter prepareForImageCapture];
self.imagetoEdit = [scaleFilter imageByFilteringImage:self.imagetoEdit];

為了平移,我這樣做。 (不是最干凈的代碼:S)也使用上面提到的UIImage + Categories。

CGFloat x_ = (translation_point.x/canvas.frame.size.width)*self.imagetoEdit.size.width;
CGFloat y_ = (translation_point.y/canvas.frame.size.height)*self.imagetoEdit.size.height;
CGFloat xx = 0;
CGFloat yy = 0;
CGFloat ww = self.imagetoEdit.size.width-x_;
CGFloat hh = self.imagetoEdit.size.height-y_;

if (translation_point.x < 0) {
    xx = x_*-1;
    ww = self.imagetoEdit.size.width + xx;
}

if (translation_point.y < 0) {
    yy = y_*-1;
    hh = self.imagetoEdit.size.height + yy;
}

CGRect cgrect = CGRectMake(xx,yy, ww, hh);
self.imagetoEdit = [self.imagetoEdit imageAtRect:cgrect];

一切似乎都有效。

這可能會有所幫助...... 以正確的方式調整UIImage的大小

可能需要對ARC等進行一些更新......雖然我認為有人已經完成並將其發布在Github上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM