需求:获取uiimage的rgb值保存到数组
内存泄漏:我用了好几个方法都出现了内存泄漏
工作效率:会在一个死循环一直调用,我尝试用消息事件来获取数据失败告终,直接获取数据失败告终!
求大神告诉我一下,感谢
运行调用这个游戏就慢慢的越来越卡越来越卡,到底是哪里没有释放掉啊,我想释放imageref但是会奔溃就直接用了,还是会越来越卡然后崩溃掉
怎么查能看内存泄漏啊,或者调试
-(CGPoint)findMultiColor:(NSString *)str x1:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2 help_simi:(float)help_simi{
NSLog(@"开始遍历:%@",str);
CGPoint p = CGPointMake(-1, -1);
NSArray *array = [str componentsSeparatedByString:@","];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
if(array.count == 1){
NSMutableArray *array2 = [[NSMutableArray alloc] init];
[array2 addObject:@0];
[array2 addObject:@0];
[array2 addObject:str];
[mutableArray addObject:array2];
//NSLog(@"mutableArray=%@,%@,%@",mutableArray[0][0],mutableArray[0][1],mutableArray[0][2]);
}
else{
for (int i=0; i<array.count; i++) {
NSArray *array2 = [array[i] componentsSeparatedByString:@"|"];
[mutableArray addObject:array2];
//NSLog(@"mutableArray=%@,%@,%@",mutableArray[i][0],mutableArray[i][1],mutableArray[i][2]);
}
}
[array release];
float rMain=0,gMain=0,bMain=0;
float rMain1=0,gMain1=0,bMain1=0;
NSArray *array1 = [mutableArray[0][2] componentsSeparatedByString:@"-"];
if(array1.count>1){
NSScanner *theScanner = [NSScanner scannerWithString:[array1 objectAtIndex:0]];
unsigned int iValue=0;
[theScanner scanHexInt:&iValue];
rMain = ((float)((iValue & 0xFF0000) >> 16));
gMain = ((float)((iValue & 0xFF00) >> 8));
bMain = ((float)(iValue & 0xFF));
NSScanner *theScanner2 = [NSScanner scannerWithString:[array1 objectAtIndex:1]];
unsigned int iValue2=0;
[theScanner2 scanHexInt:&iValue2];
rMain1 = ((float)((iValue2 & 0xFF0000) >> 16));
gMain1 = ((float)((iValue2 & 0xFF00) >> 8));
bMain1 = ((float)(iValue2 & 0xFF));
}else{
NSScanner *theScanner = [NSScanner scannerWithString:[[mutableArray objectAtIndex:0] objectAtIndex:2]];
unsigned int iValue=0;
[theScanner scanHexInt:&iValue];
rMain = ((float)((iValue & 0xFF0000) >> 16));
gMain = ((float)((iValue & 0xFF00) >> 8));
bMain = ((float)(iValue & 0xFF));
}
[array1 release];
//NSLog(@"%f,%f,%f--------%f,%f,%f",rMain,gMain,bMain,rMain1,gMain1,bMain1);
int simi=0;
if (floor(help_simi*100) / 100 ==0.9) {
simi = 9;
}else if(floor(help_simi*100) / 100 ==0.8){
simi = 18;
}else if(floor(help_simi*100) / 100 ==0.7){
simi = 29;
}else if(floor(help_simi*100) / 100 ==0.6){
simi = 38;
}else if(floor(help_simi*100) / 100 ==0.5){
simi = 49;
}
//NSLog(@"simi=%d",simi);
if(self.mainImage == NULL)
return p;
//NSLog(@"image = %@",self.mainImage);
if(self.data == NULL)
return p;
//NSLog(@"data2=%d",(int)strlen((char*)self.data));
//释放资源
// CGColorSpaceRelease(colorspace);
// CGContextRelease(cgcnt);
NSLog(@"遍历data");
int offset = 0;
float red = 0,green = 0,blue = 0;
// offset = 4*((self.width*round(100))+round(100));
// red = self.data[offset+1];
// green = self.data[offset+2];
// blue = self.data[offset+3];
// //0xd97358
// rMain = ((float)((0xd97358 & 0xFF0000) >> 16));
// gMain = ((float)((0xd97358 & 0xFF00) >> 8));
// bMain = ((float)(0xd97358 & 0xFF));
// NSLog(@" %f,%f,%f=====%f,%f,%f",red,green,blue,rMain,gMain,bMain);
// return p;
for (int m=y1; m<y2; m++) {
for (int n=x1; n<x2; n++) {
offset = 4*((self.width*round(m))+round(n));
red = self.data[offset+1];
green = self.data[offset+2];
blue = self.data[offset+3];
//NSLog(@"x=%d,y=%d %f<=%f,%d",n,m,red,rMain,offset);
if(fabsf(red-rMain)<=(rMain1+simi) && fabsf(green-gMain)<=(gMain1+simi) && fabsf(blue-bMain)<=(bMain1+simi) ){
//NSLog(@"x=%d,y=%d",n,m);
if(mutableArray.count == 1){
//NSLog(@"单点找色释放data x=%d,y=%d RGB %f %f %f",n,m,red,green,blue);
// When finished, release the context
//CGContextRelease(cgctx);
// Free image data memory for the context
//if (data) { free(data); }
[mutableArray release];
p.x = n;
p.y = m;
return p;
}else{
int max=0;
float rValue=0,gValue=0,bValue=0;
float rValue1=0,gValue1=0,bValue1=0;
for (int i=0; i<mutableArray.count; i++) {
NSArray *array = [mutableArray[i][2] componentsSeparatedByString:@"-"];
if(array.count>1){
NSScanner *theScanner = [NSScanner scannerWithString:[array objectAtIndex:0]];
unsigned int iValue=0;
[theScanner scanHexInt:&iValue];
rValue = ((float)((iValue & 0xFF0000) >> 16));
gValue = ((float)((iValue & 0xFF00) >> 8));
bValue = ((float)(iValue & 0xFF));
NSScanner *theScanner2 = [NSScanner scannerWithString:[array objectAtIndex:1]];
unsigned int iValue2=0;
[theScanner2 scanHexInt:&iValue2];
rValue1 = ((float)((iValue2 & 0xFF0000) >> 16));
gValue1 = ((float)((iValue2 & 0xFF00) >> 8));
bValue1 = ((float)(iValue2 & 0xFF));
}else{
NSScanner *theScanner = [NSScanner scannerWithString:[[mutableArray objectAtIndex:i] objectAtIndex:2]];
unsigned int iValue=0;
[theScanner scanHexInt:&iValue];
rValue = ((float)((iValue & 0xFF0000) >> 16));
gValue = ((float)((iValue & 0xFF00) >> 8));
bValue = ((float)(iValue & 0xFF));
}
int px=n+[mutableArray[i][0] intValue];
int py=m+[mutableArray[i][1] intValue];
if(px>=x2 || py>=y2 || px < 0 || py < 0){
//NSLog(@"过界break px=%d,py=%d---\nx=%d,y=%d---\nn=%d,m=%d----\nx2=%d,y2=%d",px,py,[mutableArray[i][0] intValue],[mutableArray[i][1] intValue],n,m,x2,y2);
break;
}
int offset1 = 4*((self.width*round(py))+round(px));
float red1 = self.data[offset1+1];
float green1 = self.data[offset1+2];
float blue1 = self.data[offset1+3];
if(fabsf(red1-rValue)<=(rValue1+simi) && fabsf(green1-gValue)<=(gValue1+simi) && fabsf(blue1-bValue)<=(bValue1+simi) ){
max++;
//NSLog(@"%d x=%d,y=%d RGB %f %f %f",max,n,m,red1,green1,blue1);
}
}
if(max == mutableArray.count){
NSLog(@"找到了完全释放data x=%d,y=%d RGB %f %f %f",n,m,red,green,blue);
// When finished, release the context
//CGContextRelease(cgctx);
// Free image data memory for the context
//if (data) { free(data); }
[mutableArray release];
p.x = n;
p.y = m;
return p;
}
}// if mutable==1
}
}
}
NSLog(@"没能找到释放data");
// When finished, release the context
//CGContextRelease(cgctx);
// Free image data memory for the context
//if (data) { free(data); }
[mutableArray release];
return p;
}
就是这个函数崩溃,循环data太多次了就崩溃,应该怎么办
up谁帮帮忙吧,感谢了
opencv可以根据某点查找像素怎么做啊
self.data=malloc()
这个里面没有必要每次都去重新申请一个内存
这个函数没有问题的,解决了这个函数就贡献了,我还有一个高级的算法哈哈
说下 我个人建议吧。
第一,每个函数尽量不要超过30行。自己回头看也清晰,别人看你代码也清晰。
如果一个函数太长…没心思看下去…更别说找问题
第二点嘛,就是层次化明了点,一个算法的清晰执行下去。出了问题的话,也一目了然
第三点嘛,既然是搞图色模拟,建议去 玩玩触动,想自己玩的话,可以去逆他,也可以去 github上去搜索别人的开源软件,,其中就有很多文章项目,关于图色处理的
第四点嘛,就是提问题,尽量明确化…丢一串代码…说个表象…谁都没办法解决
这是个骗人钱的骗子
… 骗子,尽然混到技术堆了