2013-04-30 2 views
1

내 뷰 컨트롤러에서 uiimageview의 일부를 자르고 싶습니다. 나는 그 위에 직사각형을 만들고있다 :직사각형 오버레이에서 uiimageView 자르기 방법?

 UIGraphicsBeginImageContext(self.view.bounds.size); 

     CGContextRef context = UIGraphicsGetCurrentContext(); 

     CGContextMoveToPoint(context, newPoint1.x, newPoint1.y); 
     CGContextAddLineToPoint(context, newPoint1.x, newPoint2.y); 
     CGContextAddLineToPoint(context, newPoint2.x, newPoint2.y); 
     CGContextAddLineToPoint(context, newPoint2.x, newPoint1.y); 
     CGContextAddLineToPoint(context, newPoint1.x, newPoint1.y); 
     CGContextClosePath(context); 

     UIColor *blue = [UIColor colorWithRed: (0.0/255.0) green: (0.0/255.0) blue: (255.0/255.0) alpha:0.4]; 
     CGContextSetFillColorWithColor(context, blue.CGColor); 

     CGContextDrawPath(context, kCGPathFillStroke); 

나는 그것을 자르는 법을 알아낼 수 없다.

UIImage *cropImage = UIGraphicsGetImageFromCurrentImageContext(); 
     rectImage = cropImage; 

     UIGraphicsEndImageContext(); 

     UIImageCrop *rectImageView = [[UIImageCrop alloc]initWithImage:rectImage]; 

     [self.view addSubview:rectImageView]; 

그래서 나는 그것에 대해보고 싶었어요 뭔가가 있다는 것을 알고 있어요, 도움 : 그것은 내 사각형 완전히 빈 : 나는 화면 캡처를 검색 할 수 있어요?

답변

4

다음을 사용하여 자른 이미지를 얻을 수 있습니다

- (UIImage*) getCroppedImage { 
    CGRect rect = PASS_YOUR_RECT; 

    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // translated rectangle for drawing sub image 
    CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, your_image.size.width, your_image.size.height); 

    // clip to the bounds of the image context 
    // not strictly necessary as it will get clipped anyway? 
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height)); 

    // draw image 
    [your_image drawInRect:drawRect]; 

    // grab image 
    UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return croppedImage; 
} 

은 당신을 도움이되기를 바랍니다.

+0

감사합니다 도움이 될 것입니다. rect.origin.x 빼기가 왜 // 직사각형으로 변환 되었습니까? – user281300

+0

매우 감사드립니다. 이것은 나를 위해 속임수를했다. – SuperKevin

5
- (UIImage *)captureScreenInRect:(CGRect)captureFrame 
{ 

    CALayer *layer; 
    layer = self.view.layer; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame); 
    [layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return screenImage; 
} 

이 그냥 참조 변경입니다 귀하의 요구 사항

희망에 따라이 코드이 코드를 이해하려고 노력 당신에게

관련 문제