我讓用戶從相機捕捉圖像或從庫中選取一個圖像。 這張圖片以UIImageView
顯示。 用戶現在可以在邊界框內縮放和定位圖像,就像使用UIImagePickerController
時allowsEditing
設置爲YES
一樣。從轉換的UIImageView中裁剪UIImage
當用戶對結果滿意並點擊完成我想製作一個剪裁的UIImage
。 使用CGImageCreateWithImageInRect
時出現問題,因爲這不考慮擴展。轉換應用於imageView,如下所示:
CGAffineTransform transform = CGAffineTransformScale(self.imageView.transform, newScale, newScale);
[self.imageView setTransform:transform];
使用gestureRecognizer。
我假定發生了什麼事情; UIImageView被縮放和移動,然後將UIViewContentModeScaleAspectFit應用於UIImage,並且當我要求它裁剪圖像時,它確實做到了 - 不考慮縮放定位。我認爲這是因爲如果我不縮放或移動圖像,而是直接點擊完成剪切工作即可。
我裁剪圖像,像這樣:
- (UIImage *)cropImage:(UIImage*) img toRect:(CGRect)rect {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale>1.0) {
rect = CGRectMake(rect.origin.x*scale , rect.origin.y*scale, rect.size.width*scale, rect.size.height*scale);
}
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.imageView.image.scale orientation:self.imageView.image.imageOrientation];
// UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return result;
}
從一種觀點認爲是我的主要觀點的一個子視圖(正方形疊加框,就像在的UIImagePickerController)在cropRect傳遞。主要的UIView有一個可縮放的UIImageView和一個顯示裁剪矩形的UIView。
我怎樣才能得到「你看到的是你得到的」剪裁,以及我必須考慮哪些因素。或者,也許建議,如果我應該實施層次或縮放不同。
我堆棧在同一個問題,你有一個解決方案? –