2012-10-02 40 views
12

我有一个应用程序,需要一个UIImageView的截图用下面的代码:iOS的截图部分

-(IBAction) screenShot: (id) sender{ 

UIGraphicsBeginImageContext(sshot.frame.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil); 


} 

这工作很好,但我需要能够位置,我采取的截图基本上我只需要屏幕三分之一的屏幕(中心部分)。我试着用

UIGraphicsBeginImageContext(CGSize 150,150); 

但已经发现,每一件事情是从0,0坐标拍摄,有没有人任何想法如何正确定位这一点。

+0

同样的问题发生在我身上我也找不到任何解决方案。 –

回答

29

那么这张截图是从你画的画布上截取的。 所以不是你画一层在整个上下文中,具有参考左上角,要采取截图你会得出它....

//first we will make an UIImage from your view 
UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//now we will position the image, X/Y away from top left corner to get the portion we want 
UIGraphicsBeginImageContext(sshot.frame.size); 
[sourceImage drawAtPoint:CGPointMake(-50, -100)]; 
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil); 
+0

谢谢你的帮助。 – yeha

+2

什么是'sshot.frame.size'?你的意思是'self.view.frame.size'吗? – progrmr

+0

这是截图的大小。您可以将该行更改为'UIGraphicsBeginImageContext(CGSizeMake(200,200));'制作一张200X200的截图图片 – Lefteris

14

this

UIGraphicsBeginImageContext(sshot.frame.size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 150, 150); // <-- shift everything up to required position when drawing. 
[self.view.layer renderInContext:c]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
+0

是的,这实际上是一个更好的解决方案 – Lefteris

+0

这似乎是一个很好的解决方案。 –

7

使用此方法来裁剪如果u有specfic矩形图像裁剪:

-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect); 
    UIImage *img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return img; 
} 

使用这样的:

UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)]; //example 
+0

谢谢你的帮助。 – yeha

0

如果你喜欢,你可以参考this的代码。

在这个例子中,您可以从任何位置和任何缩放比例获得矩形覆盖的图像。

快乐编码:)

参考一些提取的代码是如下

用于裁剪照片

- (UIImage *) croppedPhoto 
{ 
    CGFloat ox = self.scrollView.contentOffset.x; 
    CGFloat oy = self.scrollView.contentOffset.y; 
    CGFloat zoomScale = self.scrollView.zoomScale; 
    CGFloat cx = (ox + self.cropRectangleButton.frame.origin.x + 15.0f) * 2.0f/zoomScale; 
    CGFloat cy = (oy + self.cropRectangleButton.frame.origin.y + 15.0f) * 2.0f/zoomScale; 
    CGFloat cw = 300.0f/zoomScale; 
    CGFloat ch = 300.0f/zoomScale; 
    CGRect cropRect = CGRectMake(cx, cy, cw, ch); 

    NSLog(@"---------- cropRect: %@", NSStringFromCGRect(cropRect)); 
    NSLog(@"--- self.photo.size: %@", NSStringFromCGSize(self.photo.size)); 

    CGImageRef imageRef = CGImageCreateWithImageInRect([self.photo CGImage], cropRect); 
    UIImage *result = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    NSLog(@"------- result.size: %@", NSStringFromCGSize(result.size)); 

    return result; 
} 

细节如何使用例如主要功能或代码给出here

享受编码:)