屏幕上截图

来源:互联网 发布:2017淘宝游戏专营 编辑:程序博客网 时间:2024/04/30 04:40
//- (UIImage *)returnScreenshotImageViewWithScale:(CGFloat)scale
//获取keyWindow.layer
- (void)returnScreenshotImageViewWithScale:(CGFloat)scale
{

    UIGraphicsBeginImageContextWithOptions(CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height), YES, scale);
    
    [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
      NSLog(@"1---%@",[UIApplication sharedApplication].keyWindow);
    NSLog(@"1---%f---%f",image.size.width,image.size.height);
    
//        NSLog(@"---%zd",self.cutScreenData.length);
    //保存到相册
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
        image = nil;
//    return image;

}


// 从view上截图
//- (UIImage *)getImage {
- (void)getImageFromView{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), YES, 1.0);  //NO,YES 控制是否透明
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // 生成后的image
    NSLog(@"2---%f---%f",image.size.width,image.size.height);
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    image = nil;
//    return image;
}


// 根据给定得图片,从其指定区域截取一张新得图片
//-(UIImage *)getImageFromImage{
- (void)getImageFromImage{
    //大图bigImage
    //定义myImageRect,截图的区域
    CGRect myImageRect = CGRectMake(70, 10, 30, 30);
    UIImage* bigImage= [UIImage imageNamed:@"girl"];
    CGImageRef imageRef = bigImage.CGImage;
    //大图尺寸,小图尺寸
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
    CGSize size;
    size.width = 30;
    size.height = 30;
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //内容,小图尺寸、内容差()
    CGContextDrawImage(context, myImageRect, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    
    NSLog(@"3---%f---%f",smallImage.size.width,smallImage.size.height);
    UIImageWriteToSavedPhotosAlbum(smallImage, self, nil, nil);
    smallImage = nil;
//    return smallImage;
}


//图片等比例缩放
-(UIImage*)scaleToSize:(CGSize)size{
    CGFloat width = CGImageGetWidth(self.CGImage);
    CGFloat height = CGImageGetHeight(self.CGImage);
    
    float verticalRadio = size.height*1.0/height;
    float horizontalRadio = size.width*1.0/width;
    
    float radio = 1;
    if(verticalRadio>1 && horizontalRadio>1)
    {
        radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
    }
    else
    {
        radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
    }
    
    width = width*radio;
    height = height*radio;
    
    int xPos = (size.width - width)/2;
    int yPos = (size.height-height)/2;
    
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(xPos, yPos, width, height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}

0 0
原创粉丝点击