【问题标题】:Ios taking snapshot of a screen after cropping some partsIos 在裁剪某些部分后拍摄屏幕快照
【发布时间】:2023-03-11 07:10:01
【问题描述】:

是的,正如标题所说,我需要对我的应用进行裁剪快照。

我想稍微截取屏幕截图的顶部 (%20) 我已经有一个代码,用于拍摄快照并将其发送到 facebook 并且它可以正常工作,但是它会拍摄所有屏幕的照片,所以怎么能告诉我的代码忽略屏幕的 %20%。也许还有高度和宽度我在堆栈溢出中查看了一些问题并设法滑动我的屏幕截图,所以我摆脱了顶部不需要的部分,但这次在底部出现了巨大的白色区域,所以它没有解决我的问题。

这是我的快照代码

UIGraphicsBeginImageContext(self.ekran.bounds.size);
    [self.ekran.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

【问题讨论】:

    标签: ios image crop


    【解决方案1】:

    一种裁剪图像的方法,它接受任何帧来裁剪图像

    - (UIImage *)cropImage:(UIImage *)imageToCrop toRect:(CGRect)rect
        {    
            CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
            UIImage *cropped = [UIImage imageWithCGImage:imageRef];
            CGImageRelease(imageRef);
    
            return cropped;
        }
    

    如下使用:

    UIGraphicsBeginImageContext(self.ekran.bounds.size);
    [self.ekran.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGFloat imgHight = resultingImage.size.height;
    
    // Create a frame that crops the top 20% of the image
    CGRect* imageFrame = CGRectMake(0, imgHight -  (imgHight*0.8), width, imgHight*0.8);
    
    resultingImage = [self cropImage:resultingImage toRect:imageFrame];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多