zoukankan      html  css  js  c++  java
  • 在arc模式下 CGImage 释放问题

     //大图bigImage


        //定义myImageRect,截图的区域

        if (imagecount >= 3) {

            CGRect myImageRect;

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

                myImageRect = CGRectMake(0.0150.0, i.size.width, i.size.width);

            }

            else

                myImageRect = CGRectMake(0.0150.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.0150.0, i.size.width, i.size.width*2/3);

            }

            else

                myImageRect = CGRectMake(0.0150.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的对象 都需要手动来进行释放 
  • 相关阅读:
    NOIP201* 游记
    [Luogu3378] 【模板】堆 题解
    [BZOJ5105]【[Code+#1]晨跑】 题解
    牛客多校第九场 E All men are brothers 并查集/组合论
    牛客多校第九场 D Knapsack Cryptosystem 背包
    牛客多校第九场 B Quadratic equation 模平方根
    hdu多校第八场 1011 (hdu6667) Roundgod and Milk Tea 二分图匹配
    hdu多校第八场 1010(hdu6666) Quailty and CCPC 排序/签到
    hdu多校第八场 1003 (hdu6659) Acesrc and Good Numbers 数论/打表
    hdu多校第八场 1009 (hdu6665) Calabash and Landlord 计算几何/dfs
  • 原文地址:https://www.cnblogs.com/melons/p/5791963.html
Copyright © 2011-2022 走看看