12

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

-(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 坐标,有人知道如何正确定位它。

4

4 回答 4

30

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

//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);
于 2012-10-02T09:45:07.583 回答
14

从此_

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);
于 2012-10-02T09:45:33.057 回答
7

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

-(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
于 2012-10-02T09:57:16.180 回答
0

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

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

快乐编码:)

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

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

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

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

享受编码:)

于 2012-10-02T11:25:19.773 回答