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