UIGraphicsBeginImageContext UIGraphicsGetImageFromCurrentImageContext 压缩,修改,截取图片

//获取图片的缩略图

+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize{

    UIImage *newimage;

    if (nil == image) {

        newimage = nil;

    }

    else{



        CGSize oldsize = image.size;

        CGRect rect;

        if (asize.width/asize.height > oldsize.width/oldsize.height) {

            rect.size.width = asize.height*oldsize.width/oldsize.height;

            rect.size.height = asize.height;

            rect.origin.x = (asize.width - rect.size.width)/2;

            rect.origin.y = 0;

        }

        else{

            rect.size.width = asize.width;

            rect.size.height = asize.width*oldsize.height/oldsize.width;

            rect.origin.x = 0;

            rect.origin.y = (asize.height - rect.size.height)/2;

        }

        UIGraphicsBeginImageContext(asize);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);

        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background

        [image drawInRect:rect];

        newimage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

    return newimage;

}





//获取等比例压缩的图像
+ (UIImage *)getPicZoomImage:(UIImage *)image {
    
    UIImage *img = image;
    
    int h = img.size.height;
    int w = img.size.width;
    if(h <= PicAfterZoomWidth && w <= PicAfterZoomHeight)
    {
        image = img;
    }
    else
    {
        float b = (float)PicAfterZoomWidth/w < (float)PicAfterZoomHeight/h ? (float)PicAfterZoomWidth/w : (float)PicAfterZoomHeight/h;
        CGSize itemSize = CGSizeMake(b*w, b*h);
        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0, 0, b*w, b*h);
        [img drawInRect:imageRect];
        img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return img;
}


/**
 * 修改图片大小
 */
+ (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{
    newSize.height=image.size.height*(newSize.width/image.size.width);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}

//截取图片 根据frame
- (NSArray *)captureViews:(UIView *)theView frame:(NSArray *)fra{
    @autoreleasepool {
        NSMutableArray *imageViewArrReturn = [NSMutableArray array];
        UIGraphicsBeginImageContext(theView.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [theView.layer renderInContext:context];
        theView.layer.contents =nil;
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        for (int i = 0; i < fra.count; i++) {
            NSValue *frame = [fra objectAtIndex:i];
            
        
            CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, [frame CGRectValue]);
            UIImage *image = [UIImage imageWithCGImage:ref];
            [imageViewArrReturn addObject:image];
             CGImageRelease(ref);
        }
       
        CGContextRelease(context);
        return imageViewArrReturn;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值