从另一个类捕获一个View Controller的图像

时间:2013-08-01 10:55:51

标签: ios uigraphicscontext

我有一个MainViewController,最重要的是我添加了UIView iSnap,这是另一个类。

现在,我想在类MainViewController中捕获iSnap视图的一部分。 有可能吗?

我知道如何从当前View Controller的视图中捕获图像,但是我想在MainViewController类中捕获iSnap的视图部分。

此代码用于捕获驻留在iSnap Class中的图像。

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.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

1 个答案:

答案 0 :(得分:0)

得到答案:

 UIView *parentView = self.superview; //MainViewController is the superview of iSnap

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);
[parentView.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);