xcode iOS截图部分屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12687909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:51:42  来源:igfitidea点击:

iOS Screenshot part of the screen

objective-ciosxcode

提问by yeha

I have an App that takes a screenshot of a UIImageView with the following code:

我有一个应用程序,它使用以下代码截取 UIImageView 的屏幕截图:

-(IBAction) screenShot: (id) sender{

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


}

This works well but I need to be able to position where I take the screenshot basically I need to grad only a third of the screen (center portion). I tried using

这很有效,但我需要能够定位我截取屏幕截图的位置,基本上我只需要对屏幕的三分之一(中心部分)进行分级。我尝试使用

UIGraphicsBeginImageContext(CGSize 150,150);

But have found that every thing is taken from 0,0 coordinates, has anyone any idea how to position this correctly.

但是发现每件事都是从 0,0 坐标中获取的,有没有人知道如何正确定位它。

回答by Lefteris

Well the screenshot is taken from a canvas you draw. So instead of drawing your layer in the whole context, with a reference to top left corner, you will draw it where you want to take the screenshot....

好吧,屏幕截图取自您绘制的画布。因此,不是在整个上下文中绘制图层,而是参考左上角,而是将其绘制在要截取屏幕截图的位置....

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

回答by applefreak

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

回答by Paresh Navadiya

Use this method to crop if u have image with specfic rect to crop:

如果您有要裁剪的特定矩形的图像,请使用此方法进行裁剪:

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

Use like this:

像这样使用:

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

回答by The iOSDev

If you like you can refer thiscode.

如果您愿意,可以参考代码。

In this example you can get the image covered by the rect from any position and any zoom scale.

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

Happy Coding :)

快乐编码:)

Some extracted code for reference is as below

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

Main function or code used to crop the photo

用于裁剪照片的主要功能或代码

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

The details how to use the example is given here.

此处给出如何使用该示例的详细信息。

Enjoy Coding :)

享受编码:)