`

UIImage 占用内存大小

 
阅读更多

1、查看 AFNetworking 代码,里面的估算方法是

CGSize imageSize = CGSizeMake(image.size.width * image.scale, image.size.height * image.scale);
    CGFloat bytesPerPixel = 4.0;
    CGFloat bytesPerRow = imageSize.width * bytesPerPixel; // 2560
    CGFloat totalBytes = (UInt64)bytesPerPixel * (UInt64)bytesPerRow;
    NSLog(@"s1:%lu",(unsigned long)totalBytes); // 输出 10240

 2、查看 CGImage 类,发现下面的方法:CGImageGetHeight和CGImageGetBytesPerRow。这样计算应该更准确

CGFloat cgImageBytesPerRow = CGImageGetBytesPerRow(image.CGImage); // 2560
    CGFloat cgImageHeight = CGImageGetHeight(image.CGImage); // 1137
    NSUInteger size  = cgImageHeight * cgImageBytesPerRow;
    NSLog(@"size:%lu",(unsigned long)size); // 输出 2910720

 

 https://www.jianshu.com/p/634c022cb560

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics