Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从屏幕捕获中删除状态栏条_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Ios 从屏幕捕获中删除状态栏条

Ios 从屏幕捕获中删除状态栏条,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,当我用下面的代码以编程方式拍摄屏幕快照时,它会在状态栏应该位于的位置留下一个白色的条带。我知道你无法捕获状态栏,但我只想剪掉白色的空白条 - (UIImage*)captureView:(UIView *)view { CALayer *layer = [[UIApplication sharedApplication] keyWindow].layer; CGFloat scale = [UIScreen mainScreen].scale; UIGraphicsBeginImageConte

当我用下面的代码以编程方式拍摄屏幕快照时,它会在状态栏应该位于的位置留下一个白色的条带。我知道你无法捕获状态栏,但我只想剪掉白色的空白条

- (UIImage*)captureView:(UIView *)view
{
CALayer *layer = [[UIApplication sharedApplication] keyWindow].layer;
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, scale);

[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;

}

我重复了另一个问题的答案,它应该可以解决您的问题:

我以前在小班下面写过这篇文章。你可以利用它。后一个函数可以获得整个屏幕的截图(它是从苹果指南中获得的,所以它绝对是安全的)。我添加的第一部分处理不同的尺度(视网膜或规则)。愿它有帮助

#import "ScreenshotTaker.h"
#import <QuartzCore/QuartzCore.h>

@implementation ScreenshotTaker


+(UIImage *) captureRectOfScreen:(CGRect) rect
{
    UIImage *wholeScreen = [ScreenshotTaker screenshot];

    //Add status bar height
    rect.origin.y += UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ? [[UIApplication sharedApplication] statusBarFrame].size.width : [[UIApplication sharedApplication] statusBarFrame].size.height;

    //NSLog(@"%@",NSStringFromCGSize([wholeScreen size]));

    CGFloat scale = wholeScreen.scale;

    rect.origin.x *= scale;
    rect.origin.y *= scale;
    rect.size.width *= scale;
    rect.size.height *= scale;

    UIImage *cropped = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([wholeScreen CGImage], rect) scale:wholeScreen.scale orientation:wholeScreen.imageOrientation];

    //NSLog(@"Whole Screen Capt :%@ Scale: %f",NSStringFromCGSize([wholeScreen size]), wholeScreen.scale);
    //NSLog(@"Rect to Crop :%@ Cropped :%@",NSStringFromCGRect(rect), NSStringFromCGSize([cropped size]));

    return cropped;
}

+(UIImage *) screenshot
{
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

@end
#导入“ScreenshotTaker.h”
#进口
@实施截屏员
+(UIImage*)CaptureRectof屏幕:(CGRect)rect
{
UIImage*批发屏幕=[截屏者截屏];
//添加状态栏高度
rect.origin.y+=UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)?[[UIApplication sharedApplication]statusBarFrame]。size.width:[[UIApplication sharedApplication]statusBarFrame]。size.height;
//NSLog(@“%@”、NSStringFromCGSize([整屏大小]);
CGFloat scale=全屏幕.scale;
矩形原点x*=刻度;
矩形原点y*=刻度;
rect.size.width*=比例;
rect.size.height*=比例;
UIImage*裁剪=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([WhistScreen CGImage],rect)比例:WhistScreen.scale方向:WhistScreen.imageOrientation];
//NSLog(@“全屏字幕:%@比例:%f”,NSStringFromCGSize([整屏尺寸]),整屏.Scale);
//NSLog(@“Rect to Crop:%@croped:%@”、NSStringFromCGRect(Rect)、NSStringFromCGSize([裁剪大小]);
复种;
}
+(UIImage*)屏幕截图
{
//创建具有目标大小的图形上下文
//在iOS 4及更高版本上,使用UIGraphicsBeginImageContextWithOptions可将比例考虑在内
//在4之前的iOS上,回退使用UIGraphicsBeginImageContext
CGSize imageSize=[[UIScreen mainScreen]界限].size;
if(NULL!=UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(图像大小,编号,0);
其他的
UIGraphicsBeginImageContext(图像大小);
CGContextRef context=UIGraphicsGetCurrentContext();
//从后向前遍历每个窗口
对于(在[[UIApplication sharedApplication]窗口]中的UIWindow*窗口)
{
如果(![window respondsToSelector:@selector(屏幕)]| |[window screen]===[UIScreen Main screen])
{
//-RenderContext:在图层的坐标空间中渲染,
//因此,我们必须首先将层的几何体应用于图形上下文
CGContextSaveGState(上下文);
//围绕窗口的定位点将上下文居中
CGContextTranslateCm(上下文,[窗口中心].x,[窗口中心].y);
//围绕定位点应用窗口的变换
CGContextConcatCTM(上下文,[窗口转换]);
//由定位点左侧和上方的边界部分偏移
CGContextTranslateCm(上下文,
-[window bounds].size.width*[[window layer]anchorPoint].x,
-[窗口边界].size.height*[[窗口层]锚点].y);
//将层层次渲染到当前上下文
[[window layer]renderInContext:context];
//恢复上下文
CGContextRestoreGState(上下文);
}
}
//检索屏幕截图图像
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回图像;
}
@结束

太好了!把裁剪尺寸弄得乱七八糟,把我逼疯了。