2014-05-08 3 views
0

나는 ScrollView 내부에서 이미지 (imageView)를 자르는 방법을 찾기 위해 주변을 둘러 보았다. 여기에서 달성해야 할 것은 사용자가 꼬집어서 이미지를 확대하는 것입니다 (위대한 작업 중입니다). 그런 다음 확대/축소 후 화면에 표시된 이미지의 일부만자를 수 있습니다. 그래서 다른 말로하면, 내가 1에서 10까지의 숫자를 가진다면, 사용자는 그것을 단지 4와 5 만 보이도록 확대하고, 사용자가자를 때 나는 작물의 시간에 볼 수있는 4와 5의 이미지만을 필요로합니다. 이 코드를 사용하고 있습니다 ...핀치/줌 상태에서 UIImage 자르기

- (UIImage *)croppedImageWithImage:(UIImage *)image zoom:(CGFloat)zoom 
{ 
    CGFloat zoomReciprocal = 1.0f/zoom; 
    CGRect croppedRect = CGRectMake(self.scrollView.contentOffset.x*zoom, 
            self.scrollView.contentOffset.y*zoom, 
            image.size.width * zoomReciprocal, 
            image.size.height * zoomReciprocal); 

    CGImageRef croppedImageRef = CGImageCreateWithImageInRect([image CGImage], croppedRect); 

    UIImage* croppedImage = [[UIImage alloc] initWithCGImage:croppedImageRef scale:[image scale] orientation:[image imageOrientation]]; 

    CGImageRelease(croppedImageRef); 

    return croppedImage; 
} 

그러나이 방법은 이미지를 잘라 x와 y 값은 내가 내 자르기 영역의 올바른 x와 y를 얻으려면 어떻게해야합니까 무엇 ... 잘못 ?

참고하시기 바랍니다 .. 제가 자르기를 시도하는 이미지가 2200 * 3200 일 경우 어떤 차이가 있는지 확실하지 않습니까?

답변

3

나는 그것을 풀었다! 이 문제를 해결할 수있는 다른 사람들을 위해 업데이트 된 방법이 있습니다.

- (UIImage *)croppedImageWithImage:(UIImage *)image zoom:(CGFloat)zoom { 
    CGFloat zoomReciprocal = 1.0f/zoom; 
    CGFloat xOffset = image.size.width/self.scrollViewBackground.contentSize.width; 
    CGFloat yOffset = image.size.height/self.scrollViewBackground.contentSize.height; 

    CGRect croppedRect = CGRectMake(self.scrollViewBackground.contentOffset.x * xOffset, 
            self.scrollViewBackground.contentOffset.y * yOffset, 
            image.size.width * zoomReciprocal, 
            image.size.height * zoomReciprocal); 

    CGImageRef croppedImageRef = CGImageCreateWithImageInRect([image CGImage], croppedRect); 
    UIImage *croppedImage = [[UIImage alloc] initWithCGImage:croppedImageRef scale:[image scale] orientation:[image imageOrientation]]; 
    CGImageRelease(croppedImageRef); 
    return croppedImage; 

}