美文网首页ios 开发资源整合
IOS 下UIGraphicsGetImageFromCurre

IOS 下UIGraphicsGetImageFromCurre

作者: Daimer | 来源:发表于2017-02-10 15:14 被阅读1855次

    1)CGImageRef的释放

    You don't own theCGImageRefrawImageRefbecause you obtain it using[image CGImage]. So you don't need to release it.

    However, you ownrawPixelDatabecause you obtained it usingCGDataProviderCopyDataand must release it.

    CGDataProviderCopyData

    Return Value: A new data object containing a copy of the provider’s data. You are responsible for releasing this object.

    2)UIGraphicsGetImageFromCurrentImageContext

    查了N多资料,寻找到了另一种替代方法,效果要好一些,但也没办法彻底解决这个问题

    CGImageRef cgImage2 = CGBitmapContextCreateImage(ctx);

    UIImage *fixed = [UIImage imageWithCGImage:cgImage2];

    CGImageRelease(cgImage2);

    3)最终解决办法:

    因为是修改别人的代码,优化时无意中解决了此问题,原来的调用方法是在A函数中调用UIGraphicsGetImageFromCurrentImageContext(在C函数中)生成UIImage,然后传递给B函数去处理,这样内存会暴涨。现在将C函数调用生成UIImage放到B函数中生成并使用,这样就能得到及时释放。

    4)额外的建议

    因出问题应用模块是开启摄像头在不停检测每一帧图像并检测人脸,这样每秒会处理30次,导致临时的UIImage对象迅速增多,占用内存过大。在收到didReceiveMemoryWarning通知时,调用AVCaptureSession类的停止运行,然后再重新运行。

    [self.captureSessionstopRunning];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1)),dispatch_get_main_queue(), ^{

    [self.captureSessionstartRunning];

    });

    相关文章

      网友评论

      • 午马丶:你好我用这个方法 将view 转换为image为什么 会变模糊,希望回复谢谢
        Daimer:@A丶 为什么要用View 转成image?
      • 48d063e3b39b:UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.imageV.bounds.size.width, self.imageV.bounds.size.height), YES, [UIScreen mainScreen].scale);
        NSLog(@"%@",NSStringFromCGRect(self.imageV.frame));
        // self.imageV.backgroundColor = [UIColor clearColor];
        NSLog(@"%@",NSStringFromCGRect(self.imageV.frame));
        CGContextRef ref = UIGraphicsGetCurrentContext();
        NSLog(@"%@",NSStringFromCGRect(self.imageV.frame));
        [self.imageV.layer renderInContext:ref];
        NSLog(@"%@",NSStringFromCGRect(self.imageV.frame));
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        self.imageV.image = image;

        UIGraphicsEndImageContext();
        用这个方法拿到图片。为什么图片的大小会被改变啊。但是打印出来的frame是不变的。实际的大小变了。这个是为什么啊
        Daimer:@我若为女丶骚给你看 像素变小了

      本文标题:IOS 下UIGraphicsGetImageFromCurre

      本文链接:https://www.haomeiwen.com/subject/jfwgittx.html