iOS 8.1崩溃(EXC_BAD_ACCESS):<错误>:ImageIO:CGImageReadGetBytesAtOffset:***错误*** CGImageSource是使用数据大小创建的: [英] iOS 8.1 crash (EXC_BAD_ACCESS) : <Error>: ImageIO: CGImageReadGetBytesAtOffset : *** ERROR *** CGImageSource was created with data size:

查看:422
本文介绍了iOS 8.1崩溃(EXC_BAD_ACCESS):<错误>:ImageIO:CGImageReadGetBytesAtOffset:***错误*** CGImageSource是使用数据大小创建的:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢我在iOS应用中修复崩溃的任何帮助(EXC_BAD_ACCESS)。自从使用最新的iOS 8.1.2将我的应用更新到iOS 8后,我看到了这一点。相同的代码在iOS 7.x中运行良好。

Appreciate any help fixing a crash (EXC_BAD_ACCESS) in my iOS app. I am seeing this since updating my app to iOS 8, using latest available iOS 8.1.2. The same code worked fine in iOS 7.x.

我的应用程序中的一个视图是 UICollectionView 。集合视图的每个单元格都会显示一个图像,该图像会动态呈现,并在新内容可用时每隔几分钟更改一次。

One of the views in my app is a UICollectionView. Every cell of the collection view shows an image, which is rendered dynamically and changes after every few minutes as new content become available.

单元格的左上角还显示了集合视图的该单元格的文本标签/标题。为了确保文本标签可以根据单元格图像的颜色进行读取,我有一些代码(见下文)来确定单元格左上角图像的颜色。基于该颜色,我使用白色或黑色作为单元格的文本标签/标题(显示在单元格中的图像顶部)。

The top left of the cell also shows a text label/title for that cell of the collection view. To ensure the text label is readable based on what the color of the cell image is, I have some code (see below) to determine the color of the image at the top left of the cell. Based on that color, I use either white or black color for the text label/ title of the cell (displayed on top of the image in the cell).

以下是代码段。我经常在下面一行代码中遇到崩溃EXC_BAD_ACCESS崩溃:

Below is the code snippet. Quite frequently I am getting a crash EXC_BAD_ACCESS crash on the following line of code :

CGContextDrawImage(context,CGRectMake(0,0,1,1) ),imageRef);

Xcode中显示的错误日志......

Error logs shown in Xcode...


:ImageIO:CGImageReadGetBytesAtOffset: * ERROR *
CGImageSource创建时的数据大小:684918 - 当前尺寸仅为
:526399

: ImageIO: CGImageReadGetBytesAtOffset : * ERROR * CGImageSource was created with data size: 684918 - current size is only: 526399

:ImageIO:CGImageReadGetBytesAtOffset: * ERROR *
CGImageSource的创建数据大小为:203207 - 当前大小仅为
:199641

: ImageIO: CGImageReadGetBytesAtOffset : * ERROR * CGImageSource was created with data size: 203207 - current size is only: 199641

代码如下,感谢您解决此问题的任何帮助,谢谢。

The code is below, appreciate any help with solving this issue, thanks.

-(UIColor *) averageColorOfImageTitleArea {

    //CGRect croppedImageRect = CGRectMake (6,0,100,29);
    CGRect croppedImageRect = CGRectMake (0,0,1,1);

    CGImageRef imageRef = CGImageCreateWithImageInRect (self._collectionViewImage.image.CGImage,croppedImageRect);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char rgba[4];

    CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), imageRef);

    if(rgba[3] > 0) {
        CGFloat alpha = ((CGFloat)rgba[3])/255.0;
        CGFloat multiplier = alpha/255.0;
        return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier
                               green:((CGFloat)rgba[1])*multiplier
                                blue:((CGFloat)rgba[2])*multiplier
                               alpha:alpha];
    }
    else {
        return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0
                               green:((CGFloat)rgba[1])/255.0
                                blue:((CGFloat)rgba[2])/255.0
                               alpha:((CGFloat)rgba[3])/255.0];
    }

    CGContextRelease(context);
    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpace);

}


推荐答案

我是得到这个错误是因为我调用的api在tmp >> NSData文件中抛出500并返回html错误页面我试图转换为PNG。

I was getting this error because the api I called was throwing a 500 and returning html error page in the tmp >> NSData file I was trying to convert to PNG.

我假设statusCode为200且tmp为png。

I was presuming a statusCode of 200 and the tmp was png.

您可能希望检查文件在转换之前,你试图打开的是一张图像。

You may wish to check the file your trying to open is an image at all before conversion.

你可以检查文件的字节大小是199641并放入断点

You can check bytesize of file is 199641 and put in break point

这是我如何追踪CGImageSource的问题下载图像时

heres how I tracked down CGImageSource issue when downloading the image

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL){

    print("didFinishDownloadingToURL[\(location) countOfBytesReceived:\(downloadTask.countOfBytesReceived)]")

    if let httpResponse = downloadTask.response as? HTTPURLResponse{

            if httpResponse.statusCode == 200{
                //----------------------------------------------------------------------------------------
                //200 - start
                //----------------------------------------------------------------------------------------
                if (downloadTask.countOfBytesReceived == 199641){
                    //bytesize metioned in 
                    : ImageIO: CGImageReadGetBytesAtOffset : * ERROR * CGImageSource was created with data size: 203207 - current size is only: 199641

                }else{
                    //----------------------------------------------------------------------------------------
                    //Image OK
                    //----------------------------------------------------------------------------------------
                }

                //----------------------------------------------------------------------------------------
                //200 - 3nd
                //----------------------------------------------------------------------------------------
            }
            else if httpResponse.statusCode ==  500
            {
                //check the tmp file in location path
            }else{

            }
        }else{
            appDelegate.log.error("ERROR downloadTask.response NOT HTTPURLResponse for taskDescription:'\(cacheKeyString)' - 500")
        }


}

这篇关于iOS 8.1崩溃(EXC_BAD_ACCESS):<错误>:ImageIO:CGImageReadGetBytesAtOffset:***错误*** CGImageSource是使用数据大小创建的:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆