我们正在开发 ipad 应用程序,我正在通过此代码比较两个图像:
-(BOOL) compareTwoImages:(UIImage *) firstImage SecondImage:(UIImage *) secondImage
{
int count=0;
int match=0;
int mismatch=0;
// UIColor* color = nil;
CGImageRef inImage = [firstImage CGImage];
CGImageRef outImage =[secondImage CGImage];
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];//this method creates context
CGContextRef cgctx1 = [self createARGBBitmapContextFromImage:outImage];
if (cgctx == NULL) { return NO; /* error */ }
else if (cgctx1 == NULL) { return NO; /* error */ }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextFlush(cgctx);
CGContextFlush(cgctx1);
CGContextDrawImage(cgctx, rect, inImage);
CGContextDrawImage(cgctx1, rect, outImage);
CGContextFlush(cgctx);
CGContextFlush(cgctx1);
CGContextFlush(cgctx);
CGContextFlush(cgctx1);
// Now we can get a pointer to the image data associated with the bitmap
// context.
unsigned char* data = CGBitmapContextGetData (cgctx);
unsigned char* data1 = CGBitmapContextGetData (cgctx1);
if ((data != NULL)&&(data1 != NULL))
{
//offset locates the pixel in the data from x,y.
//4 for 4 bytes of data per pixel, w is width of one row of data.
for( int yy=0;yy<h;yy++)
{
for (int xx=0; xx<w; xx++)
{
int offset = 4*((w*round(yy))+round(xx));
int alpha = data[offset];
int alpha1 = data1[offset];**//it is giving bad access here and finally crashing**
if( alpha >1 )
{
count++;
if( alpha1 > 1 )
{
match++;
}
}
if( (alpha1 >1 )&& (alpha < 1))
{
mismatch++;
}
// NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha);
}
}
}
// When finished, release the context
CGContextRelease(cgctx);
CGContextRelease(cgctx1);
// Free image data memory for the context
if (data) { free(data); }
if (data1) { free(data1); }
int matchPer =(int) (( (float) match/count)*100);
int misMatchPer =(int) (( (float) mismatch/count)*100);
NSLog(@"number of match px :%d mismatch px :%d total count :%d precntage %d mismathc per %d",match,mismatch,count,matchPer,misMatchPer);
if(( matchPer>70)&&(misMatchPer <2000)) {//do anything}
当我通过此代码比较图像时,它在 ipad 中运行良好,但是当我选择模拟器硬件 >>ipad 视网膜时出现问题。
我试图通过仪器>>僵尸捕捉这种糟糕的访问,但无法理解它崩溃的原因......