2013-05-30 5 views
1

카메라 또는 갤러리에서 찍은 가로 세로 맞춤 이미지를 보여주는 이미지보기가 있습니다. Imageview 맨 위에 UIView (이동 가능, 확장 가능) 배치했습니다.iOS 앱에서 이미지 자르기

해당 UIView의 프레임을 기준으로 이미지의 영역을 자르고 싶습니다. 내가 지금 뭐하는 거지 때 문제가

CGImageRef imageRef = CGImageCreateWithImageInRect([myImageView.image CGImage], cropV.frame);// cropV is the UIview over the myImageView 

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

CGImageRelease(imageRef); 

은 (크기가 큰 것) 이미지에 내 사각형의 실제 위치 (UIView의)가 동일하지 않습니다 :

그래서 내가 이런 짓을.

예를 들어. 1024x1024 이미지의 경우 320x480 화면의 가로 세로 맞춤 모드에서 이미지가 다르게 나타날 수 있습니다. 내 cropV가 프레임 (1010300300)에있는 경우; CGImageCreateWithImageInRect는 1024x1024 이미지에 따라 이미지를 생성합니다.

이미지 크기에 따라 cropV 프레임을 조정할 수있는 방법이 있습니까? 이제까지 직사각형 (cropV)이 오직 그 부분 일 수있는 곳이 잘립니다.

현재로서는 컨텍스트에서 이미지를 그립니다. 그러나 이렇게하면 와이드 스크린/좋은 HD 사진에 대해 흐릿한 작물을 얻을 수 있습니다.

UIGraphicsBeginImageContext(myImageView.frame.size); 

[myImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], cropV.frame); 

답변

0

이미지 대신 비율을 얻고 볼 수 있습니다.

float ratio = yourImage.size.height/yourView.frame.size.height; 

wRatio_width = myImageView.image.size.width/cropV.frame.size.width; 
wRatio_height = myImageView.image.size.height/cropV.frame.size.height; 
CGRect headImageRect = cropV.frame; 
headImageRect.origin.x=wRatio_width * headImageRect.origin.x; 
headImageRect.origin.y=wRatio_height * headImageRect.origin.y; 

headImageRect.size.width=wRatio_width * headImageRect.size.width; 
headImageRect.size.height=wRatio_height * headImageRect.size.height; 

cropV.frame=headImageRect; 
+0

내 상황에서 이것을 사용하는 방법을 알아낼 수 없습니다보십시오! 당신은 그것을 제안하고, x, y, 너비와 높이에 대한 비율을 계산 한 다음, 내 cropV 프레임에 이러한 값을 곱합니까 ?? – Nil

+0

이미지 (UIImageView 아님) 및 뷰와 관련하여 배급을 받으십시오. – Rajneesh071

+0

이 작업을 수행했지만 원하는 자르기 영역이 아닌 전체 이미지가 나타납니다.이 작업 방식대로 'wRatio = myImageView.image/cropV.frame.size.width;' 그런 다음 내 cropV wide에 비율을 곱합니다. 같은 높이입니다. – Nil

관련 문제