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);
}
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
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
So if you load
returnImage
into imageIO func or func which uses CGContext, try to give the valuereturnImage
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.