今天要修改銀行卡 防微信形式的 有一個坑呀
這還是改過的 只是獲取背景顏色就是個坑 沒辦法 還好網上找到一個博主姐姐寫的 代碼
//根據圖片獲取圖片的主色調
-(UIColor*)mostColor:(UIImage*)image{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
int bitmapInfo = kCGImageAlphaPremultipliedLast;
#endif
//第一步 先把圖片縮小 加快計算速度. 但越小結果誤差可能越大
CGSize thumbSize=CGSizeMake(image.size.width/2,image.size.height/2);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
thumbSize.width,
thumbSize.height,
8,//bits per component
thumbSize.width*4,
colorSpace,
bitmapInfo);
CGRect drawRect = CGRectMake(0, 0, thumbSize.width/2, thumbSize.height/2);
CGContextDrawImage(context, drawRect, image.CGImage);
CGColorSpaceRelease(colorSpace);
//第二步 取每個點的像素值
unsigned char* data = CGBitmapContextGetData (context);
if (data == NULL) return nil;
NSCountedSet *cls=[NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];
for (int x=0; x<thumbSize.width; x++) {
for (int y=0; y<thumbSize.height; y++) {
int offset = 4*(x*y);
int red = data[offset];
int green = data[offset+1];
int blue = data[offset+2];
int alpha = data[offset+3];
if (alpha>0) {//去除透明
if (red==255&&green==255&&blue==255) {//去除白色
}else{
NSArray *clr=@[@(red),@(green),@(blue),@(alpha)];
[cls addObject:clr];
}
}
}
}
CGContextRelease(context);
//第三步 找到出現次數最多的那個顏色
NSEnumerator *enumerator = [cls objectEnumerator];
NSArray *curColor = nil;
NSArray *MaxColor=nil;
NSUInteger MaxCount=0;
while ( (curColor = [enumerator nextObject]) != nil )
{
NSUInteger tmpCount = [cls countForObject:curColor];
if ( tmpCount < MaxCount ) continue;
MaxCount=tmpCount;
MaxColor=curColor;
}
return [UIColor colorWithRed:([MaxColor[0] intValue]/255.0f) green:([MaxColor[1] intValue]/255.0f) blue:([MaxColor[2] intValue]/255.0f) alpha:([MaxColor[3] intValue]/255.0f)];
}
找到代碼 就好一些了 在要實現的cell.h 里
-(UIColor*)mostColor:(UIImage*)image;
cell.m中 把代碼復制上去
在VC里
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
LZSelectBanTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
LZSelectBanModel * model =[LZSelectBanModel mj_objectWithKeyValues:_cardArr[indexPath.row]];
[cell.iconImaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",QYBM_API_URL,model.bl_pic]]placeholderImage:[UIImage imageNamed:@"默認"]];
UIColor *c = [cell mostColor:cell.iconImaView.image];
cell.backView.backgroundColor = c;
cell.nameLbl.text =model.bl_name;
cell.titleLbl.text =model.bc_bank_num;
NSString *str = [model.bc_bank_num substringFromIndex:model.bc_bank_num.length -4];
cell.titleLbl.font = [UIFont systemFontOfSize:24];
cell.titleLbl.text = [NSString stringWithFormat:@"**** **** **** %@",str];
return cell;
}
這樣就獲取到了 這里又一個坑 第一次運行 是不出效果的 畢竟是菜鳥 找原因把 改被人的代碼就這樣 慢慢找
原來是這個里Image是用sdwebImage異步獲取的, 是在另一個線程里網絡請求的, 這樣第一次運行 是獲取不到圖片的
把里面的獲取圖片的 改成 先獲取到圖片 在獲取顏色
[cell.iconImaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",QYBM_API_URL,model.bl_pic]]placeholderImage:[UIImage imageNamed:@"默認"]];
改成
[cell.iconImaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",QYBM_API_URL,model.bl_pic]] placeholderImage:[UIImage imageNamed:@"默認"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
UIColor *c = [cell mostColor:cell.iconImaView.image];
cell.backView.backgroundColor = c;
}];
再運行,
有的 運行 會出先錯誤
我就把代碼 修改了一下
//第一步 先把圖片縮小 加快計算速度. 但越小結果誤差可能越大
CGSize thumbSize=CGSizeMake(image.size.width/2,image.size.height/2);
在這里我吧make 寫死了 你獲取的圖片是多大 就寫多大吧
CGSize thumbSize=CGSizeMake(40,40);
if (red==255&&green==255&&blue==255) 這里
我改成了
if (red<=240&&green<=240&&blue<=240)
再運行