2012-01-02 37 views
16

的截图一样,你可以在我的代码看,我采取截图并保存到相册。编程采取特定区域

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
} 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

起初我用webview.size代替self.view.bounds.size和它正常工作,因为有观点位于0/0。但是现在我集中了WebView,但对于给定尺寸,图片从0/0开始。

如何配置屏幕截图在给定大小的另一个location(例如300/150)处开始?

或者有另一种方式来采取UIWebView的照片吗?

+0

这是否在桌面OSX或只是iphone的工作? – Noitidart 2015-05-07 19:53:32

回答

2

我解决我的问题,但没有回答这个问题:

我创建的UIView子类,并动了我的UIWebView在那里,所以它相对于子类位于0/0。然后使用WebView的大小:

UIGraphicsBeginImageContext(webview.frame.size); 

和上面的其他代码。现在

您可以在子类移动到你希望每一个位置。

备注:如果您有类似日期/时间标签的标签,您也必须将其移动,否则您将无法在图片上看到它们。

因此,创建的UIView一个子类,每移动IBOutlet你想成为 看到在那里的图片。

问题仍然存在: 那么有可能拍摄一个特定的屏幕区域?

亲切的问候。 $ H @ RKY

0
- (void)drawRect:(CGRect)rect { 
    UIGraphicsBeginImageContext(self.bounds.size);  
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
} 

你可以打电话给你的观点:]

+0

创建Web视图时会调用drawRect方法 - 所以在开始时我有一张灰色的图片。 我从这得到的想法是拖动代码并将其放置在我的Web视图位于0/0的自定义UIView类中。这工作,但我没有看到标签了,因为它是“根视图”的一部分。 – Sharky 2012-01-03 07:28:56

23

你需要做两个变化,以只抓取屏幕的一部分:

  1. 创建一个更小的图像 - 使用你想拍摄的矩形的大小。
  2. 移动,你渲染成是你想拍摄的矩形负的x/y的上下文的原点。

之后,您只需将图层渲染到上下文中,然后就可以获得所需的内容。像这样的事情应该这样做:

CGRect grabRect = CGRectMake(40,40,300,200); 

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(grabRect.size); 
} 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y); 
[self.view.layer renderInContext:ctx]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
+0

这使我的应用崩溃。任何想法为什么?我已经添加了QuartzCore框架,并将这些代码放在一个函数中,但仍然没有结束。 – 2013-06-05 13:59:15

+0

@rebellion - 我已经得到这个工作。尽管不在相册中。看到我的改编如下UIView类别的方法。 – idStar 2013-06-20 22:02:20

+0

谢谢,这对我很好。只是一个小问题的图像背景颜色是白色的,我希望它透明。我怎样才能做到这一点。请帮忙 – iMemon 2013-11-06 14:22:01

10

我已经采取和测试@ escrafford的答案上面,并得到它的工作就好了。我在UIView中将它作为一个类别方法的上下文中进行了测试,以便'grabRect'可以被参数化。下面是代码,作为UIView类别,返回给你一个UIImageView:

/** 
Takes a screenshot of a UIView at a specific point and size, as denoted by 
the provided croppingRect parameter. Returns a UIImageView of this cropped 
region. 

CREDIT: This is based on @escrafford's answer at http://stackoverflow.com/a/15304222/535054 
*/ 
- (UIImageView *)rp_screenshotImageViewWithCroppingRect:(CGRect)croppingRect { 
    // For dealing with Retina displays as well as non-Retina, we need to check 
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect 
    // passed in, and not the size of the view we are taking a screenshot of. 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(croppingRect.size); 
    } 

    // Create a graphics context and translate it the view we want to crop so 
    // that even in grabbing (0,0), that origin point now represents the actual 
    // cropping origin desired: 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y); 
    [self.layer renderInContext:ctx]; 

    // Retrieve a UIImage from the current image context: 
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Return the image in a UIImageView: 
    return [[UIImageView alloc] initWithImage:snapshotImage]; 
} 
+0

这个代码当然是假设ARC,并且是iOS6.1中的当前工作解决方案。 – idStar 2013-06-20 22:04:08

+1

伟大的解决方案!谢谢! – MaKo 2013-11-05 01:02:47