0
我正在使用下面的裁剪方法來裁剪Uiimage坐在UIImageView,然後坐在UIScrollView。問題cropImage方法在UIScrollView中的圖像產生錯誤的圖像
-(UIImage *)cropImage:(UIImage *)image
{
float scale = 1.0f/_scrollView.zoomScale;
NSLog(@"Oh and heres that zoomScale: %f", _scrollView.zoomScale);
CGRect visibleRect;
visibleRect.origin.x = _scrollView.contentOffset.x * scale;
visibleRect.origin.y = _scrollView.contentOffset.y * scale;
visibleRect.size.width = _scrollView.bounds.size.width * scale;
visibleRect.size.height = _scrollView.bounds.size.height * scale;
NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.x);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.y);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.width);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], visibleRect);
UIImage *croppedImage = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
我需要將圖像裁剪爲CGSize(321,115)。在剪切圖像並看到打印結果後,我可以看到visibleRect是(0,0,321,115) - 它應該是這樣的,然後UIImage的寬度爲321,高度爲115。由於某些原因,圖像似乎完全放大得太遠了(該方法將原始圖像的一小部分裁剪爲321x115的大小)。
爲什麼此方法不能正確裁剪我的圖像?
- 作爲一個方面說明:當我調用這個方法時,我打電話給_croppedImage = [self cropImage:_imageView.image];
,它將自定義UIView類的UIImage屬性設置爲裁剪後的圖像。
這段代碼如何考慮我的UIScrollView zoomScale?我必須考慮UIScrollView的scaleFactor,否則這不會裁剪所選區域 – Chisx
這意味着您只想裁剪選定的區域。對 ?如果是,請檢查已編輯的答案 –
是的,我幾乎工作使用http://stackoverflow.com/questions/7760191/cropping-a-uiimage-according-to-the-viewport-inside-a-uiscrollview但有問題與該代碼..只有稍微關閉。是的,只有選定的區域..基本上是縮放區域或出現在UIScrollView中的區域。這很接近,但那不是我正在尋找的內容,我在這裏分享的鏈接基本上是我在尋找的內容,但它不能完全正常工作。 – Chisx