2013-05-03 149 views
-3

下面是我的代碼,我有裁剪圖像的問題,任何人都可以請解決這個問題。問題在裁剪圖像

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); 

回答

2

離開裁剪圖像

- (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; 
} 
0

試試這個這一切只需使用下面的方法。

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect 
    { 
     //create a context to do our clipping in 
     UIGraphicsBeginImageContext(rect.size); 
     CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

     //create a rect with the size we want to crop the image to 
     //the X and Y here are zero so we start at the beginning of our 
     //newly created context 
     CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
     CGContextClipToRect(currentContext, clippedRect); 

     //create a rect equivalent to the full size of the image 
     //offset the rect by the X and Y we want to start the crop 
     //from in order to cut off anything before them 
     CGRect drawRect = CGRectMake(rect.origin.x * -1, 
            rect.origin.y * -1, 
            imageToCrop.size.width, 
            imageToCrop.size.height); 

     //draw the image to our clipped context using our offset rect 
     CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage); 

     //pull the image from our cropped context 
     UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

     //pop the context to get back to the default 
     UIGraphicsEndImageContext(); 

     //Note: this is autoreleased 
     return cropped; 
    }