在arc模式下 CGImage 释放问题

一直以为 arc模式下 什么东西 都可以自动去释放 不需要自己去管理
后来才发现 根本不是那么回事 

//裁减图片

-(UIImage *)getImageFromImage:(UIImage *)i :(int)imagecount{


    //大图bigImage


    //定义myImageRect,截图的区域

    if (imagecount >= 3) {

        CGRect myImageRect;

        if (i.size.width<= i.size.height) {

            myImageRect = CGRectMake(0.0, 150.0, i.size.width, i.size.width);

        }

        else

            myImageRect = CGRectMake(0.0, 150.0, i.size.height, i.size.height);

        

        UIImage* bigImage= i;

        

        CGImageRef imageRef = bigImage.CGImage;

        

        CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

        CGSize size;

        

        size.width = 80;

        

        size.height = 80;

        

        UIGraphicsBeginImageContext(size);

        

        CGContextRef context = UIGraphicsGetCurrentContext();

        

        CGContextDrawImage(context, myImageRect, subImageRef);

        

        UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

        CGImageRelease(subImageRef);

        UIGraphicsEndImageContext();

        

        

        return smallImage;


    }

    

    if (imagecount < 3) {

        CGRect myImageRect;

        if (i.size.width<= i.size.height) {

            myImageRect = CGRectMake(0.0, 150.0, i.size.width, i.size.width*2/3);

        }

        else

            myImageRect = CGRectMake(0.0, 150.0, i.size.height, i.size.height*2/3);

        

        UIImage* bigImage= i;

        

        CGImageRef imageRef = bigImage.CGImage;

        

        CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

        CGSize size;

        

        size.width = 120;

        

        size.height = 80;

        

        UIGraphicsBeginImageContext(size);

        

        CGContextRef context = UIGraphicsGetCurrentContext();

        

        CGContextDrawImage(context, myImageRect, subImageRef);

        

        UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];   //注意蓝色的部分

        CGImageRelease(subImageRef);


        

        

        return smallImage;

        

    }

    return nil;

   

}



像这样 裁剪图片 就会有内存泄漏 

后来上 stackoverflow 查找 发现这样一段话

ARC does not manage C-types, of which CGImage may be considered. You must release the ref manually when you are finished with CGImageRelease(image);


   UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

        CGImageRelease(subImageRef);

        UIGraphicsEndImageContext();

也就是 在arc模式下 不是什么东西 都可以释放 例如  C-types的对象 都需要手动来进行释放 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值