Hi,
I'm using this following code to set blur effect for an uiimage basing on slider value. sliver value ranges from 0,5,10,15,20. THis logic is working fine in simulator but it is crashing in device. Please tell me anyone whats wrong in this logic, or is there any other logic better than this.
I'm new to this objective-c. Please help me in solving this issue.
Thanks in advance.........
This is the crash log
Program received signal: “EXC_BAD_ACCESS”.
(gdb) bt
#0 0x320d27c4 in CGSConvertABGR8888toRGBA8888 ()
#1 0x31fdb184 in argb32_image ()
#2 0x3126fb60 in ripl_Mark ()
#3 0x31272dd0 in ripl_BltImage ()
#4 0x312649c0 in RIPLayerBltImage ()
#5 0x3126954c in ripc_RenderImage ()
#6 0x3126be34 in ripc_DrawImage ()
#7 0x31fd5054 in CGContextDelegateDrawImage ()
#8 0x31fd4e78 in CGContextDrawImage ()
#9 0x31c8af38 in CA::Render::create_image_by_rendering ()
#10 0x31c8460c in CA::Render::create_image_from_image_data ()
#11 0x31c84498 in CA::Render::create_image ()
#12 0x31c84154 in CA::Render::copy_image ()
#13 0x31c8407c in CA::Render::prepare_image ()
#14 0x31c7e9a0 in CALayerPrepareCommit_ ()
#15 0x31c7e9d8 in CALayerPrepareCommit_ ()
#16 0x31c7e9d8 in CALayerPrepareCommit_ ()
#17 0x31c7e9d8 in CALayerPrepareCommit_ ()
#18 0x31c7e9d8 in CALayerPrepareCommit_ ()
#19 0x31c7e9d8 in CALayerPrepareCommit_ ()
#20 0x31c7e9d8 in CALayerPrepareCommit_ ()
#21 0x31c7e86c in CALayerPrepareCommit ()
#22 0x31c7bbd0 in CA::Context::commit_transaction ()
#23 0x31c7b7e0 in CA::Transaction::commit ()
#24 0x31c839e0 in CA::Transaction::observer_callback ()
#25 0x3223524a in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#26 0x32236da4 in __CFRunLoopDoObservers ()
#27 0x322382fc in __CFRunLoopRun ()
#28 0x321df0c2 in CFRunLoopRunSpecific ()
#29 0x321defd0 in CFRunLoopRunInMode ()
#30 0x3390ff90 in GSEventRunModal ()
#31 0x34b0ab48 in -[UIApplication _run] ()
#32 0x34b08fc0 in UIApplicationMain ()
#33 0x00002db2 in main (argc=1, argv=0x2ffff570) at /Users/Desktop/SampleProject/Shared/main.m:14
This is the calling method:
tempImgView.image = [self blurredCopyUsingGuassFactor:value andPixelRadius:value];
CGContextRef CreateARGBBitmapContext (CGImageRef inImage)
{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
return NULL;
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
CGColorSpaceRelease( colorSpace );
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
free (bitmapData);
CGColorSpaceRelease( colorSpace );
return context;
}
CGImageRef CreateCGImageByBlurringImage(CGImageRef inImage, NSUInteger pixelRadius, NSUInteger gaussFactor)
{
unsigned char *srcData, *destData, *finalData;
CGContextRef context = CreateARGBBitmapContext(inImage);
if (context == NULL)
return NULL;
size_t width = CGBitmapContextGetWidth(context);
size_t height = CGBitmapContextGetHeight(context);
size_t bpr = CGBitmapContextGetBytesPerRow(context);
size_t bpp = (CGBitmapContextGetBitsPerPixel(context) / 8);
CGRect rect = {{0,0},{width,height}};
CGContextDrawImage(context, rect, inImage);
// Now we can get a pointer to the image data associated with the bitmap
// context.
srcData = (unsigned char *)CGBitmapContextGetData (context);
if (srcData != NULL)
{
size_t dataSize = bpr * height;
finalData = malloc(dataSize);
destData = malloc(dataSize);
memcpy(finalData, srcData, dataSize);
memcpy(destData, srcData, dataSize);
int sums[gaussFactor];
int i, x, y, k;
int gauss_sum=0;
int radius = pixelRadius * 2 + 1;
int *gauss_fact = malloc(radius * sizeof(int));
for (i = 0; i < pixelRadius; i++)
{
gauss_fact[i] = 1 + (gaussFactor*i);
gauss_fact[radius - (i + 1)] = 1 + (gaussFactor * i);
gauss_sum += (gauss_fact[i] + gauss_fact[radius - (i + 1)]);
}
gauss_fact[(radius - 1)/2] = 1 + (gaussFactor*pixelRadius);
gauss_sum += gauss_fact[(radius-1)/2];
unsigned char *p1, *p2, *p3;
for ( y = 0; y < height; y++ )
{
for ( x = 0; x < width; x++ )
{
p1 = srcData + bpp * (y * width + x);
p2 = destData + bpp * (y * width + x);
for (i=0; i < gaussFactor; i++)
sums[i] = 0;
for(k=0;k<radius;k++)
{
if ((y-((radius-1)>>1)+k) < height)
p1 = srcData + bpp * ( (y-((radius-1)>>1)+k) * width + x);
else
p1 = srcData + bpp * (y * width + x);
for (i = 0; i < bpp; i++)
sums[i] += p1[i]*gauss_fact[k];
}
for (i=0; i < bpp; i++)
p2[i] = sums[i]/gauss_sum;
}
}
for ( y = 0; y < height; y++ )
{
for ( x = 0; x < width; x++ )
{
p2 = destData + bpp * (y * width + x);
p3 = finalData + bpp * (y * width + x);
for (i=0; i < gaussFactor; i++)
sums[i] = 0;
for(k=0;k<radius;k++)
{
if ((x -((radius-1)>>1)+k) < width)
p1 = srcData + bpp * ( y * width + (x -((radius-1)>>1)+k));
else
p1 = srcData + bpp * (y * width + x);
for (i = 0; i < bpp; i++)
sums[i] += p2[i]*gauss_fact[k];
}
for (i=0; i < bpp; i++)
{
p3[i] = sums[i]/gauss_sum;
}
}
}
}
size_t bitmapByteCount = bpr * height;
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, destData, bitmapByteCount, NULL);
CGImageRef cgImage = CGImageCreate(width, height, CGBitmapContextGetBitsPerComponent(context),
CGBitmapContextGetBitsPerPixel(context), CGBitmapContextGetBytesPerRow(context), CGBitmapContextGetColorSpace(context), CGBitmapContextGetBitmapInfo(context),
dataProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
CGContextRelease(context);
if (destData)
free(destData);
if (finalData)
free(finalData);
return cgImage;
}
- (UIImage *)blurredCopyUsingGuassFactor:(int)gaussFactor andPixelRadius:(int)pixelRadius
{
CGImageRef retCGImage = CreateCGImageByBlurringImage(refImage.CGImage, pixelRadius, gaussFactor);
UIImage *retUIImage = [UIImage imageWithCGImage:retCGImage];
CGImageRelease(retCGImage);
return retUIImage;
}