iOS 8.1 crash (EXC_BAD_ACCESS) : <Error>: ImageIO: CGImageReadGetBytesAtOffset : *** ERROR *** CGImageSource was created with data size:

╄→гoц情女王★ 提交于 2019-12-02 16:04:11

问题


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.

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).

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);

Error logs shown in Xcode...

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

: 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);

}

回答1:


It happened to me too. In my case while clipping a picture. My guess it was because the iCloud was suffering from a temporary networking glitch. Probably best to file a bug report.




回答2:


First of all sorry for the late answer but maybe it will help someone with a same problem. Main reason is that imageIO cant load image as fast as you need for my example i was reordering UITableViewCell. Maybe It will help someone with his logics. And try to load Image into NSData and then convert to UIImage.

For example in cellForRowAtIndexPath

  let image = UIImagePNGRepresentation(self.imageArray[indexPath.row])
  let returnImage = UIImage(data: image)

So if you load returnImage into imageIO func or func which uses CGContext, try to give the value returnImage




回答3:


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.

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.

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

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")
        }


}


来源:https://stackoverflow.com/questions/28019631/ios-8-1-crash-exc-bad-access-error-imageio-cgimagereadgetbytesatoffset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!