欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS获取指定位置图像

程序员文章站 2022-06-18 14:09:48
//指定视图截图-(UIImage *)screenShotView:(UIView *)view{ UIImage *imageRet = [[UIImage alloc]init]; UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; imageRet = ......

//指定视图截图

-(UIImage *)screenShotView:(UIView *)view{

    UIImage *imageRet = [[UIImage alloc]init];

    UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0.0);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    imageRet = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return imageRet;

}

//获得某个范围内的屏幕图像

- (UIImage *)imageFromView: (UIView *) theView   atFrame:(CGRect)rect

{

    UIGraphicsBeginImageContext(theView.frame.size);

    //用这个不会失真

//    UIGraphicsBeginImageContextWithOptions(theView.frame.size, false, 0.0);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    //这里其实就是裁剪

    UIRectClip(rect);

    [theView.layer renderInContext:context];

    //设定颜色:透明

    [[UIColor clearColor] setFill];

    

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    

    //获取 某图片 指定范围(rect)内的cgImage

    CGImageRef cgImage = CGImageCreateWithImageInRect(theImage.CGImage, rect);

    UIImage * returnImage = [UIImage imageWithCGImage:cgImage];

    CGImageRelease(cgImage);

    UIGraphicsEndImageContext();

    

    return  returnImage;

}

本文地址:https://blog.csdn.net/ios_xumin/article/details/108174043