1

I have a Main View with frame

0, 60, 768, 1024,

It has many views as subView. First subview is an UIImageView. The property associated with ImageView is AspectFit. Now I want to take screenshot of the only part of screen which bounds that of UIImageView (There are at least 4-5 Subviews which CAN or can not be above This UIImageView) The standard coding given by iPhone has been used here. The image i get is of Main View. Now to get only screenshot of UIImageView and whatever above is, I need to set the dimension of my main view to envelop only UIImageView.

Suppose The image size is 100,100. When I press button, How should I set the Bounds/fRAME OF my mainView? Or In other terms how can I get the frame of UIImageView relative to self.view

4

2 回答 2

0

Instead of changing the frame of main view, try to calculate coordinates of the UIImageView.

Assume that frames of your main view and image view are (0, 60, 768, 1024) and (20, 20, 100, 100), respectively.

So frame of your image view with respect to global coordinate system is (20, 80, 100, 100). Just add x, y coordinates of main view to its subviews to achieve this. Then, you may get the screenshot of desired frame.

于 2013-09-20T14:58:26.940 回答
0

一种方法是抓取整个视图的图像,然后对其进行裁剪。无需重构。结合此处此处的最佳答案,它可能如下所示:

- (UIImage *)imageWithView:(UIView *)view croppedBy:(CGRect)rect {

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
    return result;
}

对于您的应用程序,这样称呼它(假设我们在视图控制器中):

UIImage *justMyImageView = [self imageWithView:self.view croppedBy:self.myImageView.frame];
于 2013-09-20T15:02:08.123 回答