我有两种将图像添加到表格的方法,哪种方法最有效,还有更好的方法吗?两种方式都放置在带有“图层对象”的按钮顶部。
另外我注意到表格在模拟器中滚动缓慢,这种缓慢滚动可能发生在真手机上吗?我看到原因的唯一原因可能是因为我没有关联按钮,但它可能与记忆有关,或者在每个单元格中绘图。我不能是因为图像来自 URL 对吗?
以下是我将每个按钮添加到表格单元格的两种方法:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"image.png" forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor blackColor]];
UIImage *btn = [UIImage imageWithData:"png image from url"];
UIImage* newImage2 = [btn scaleToSize:CGSizeMake(280, 209.0)];
CGSize newSize = CGSizeMake(280, 209.0);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[newImage2 drawInRect:CGRectMake(0, 0, (newSize.width), (newSize.height))];
UIImage *newImage4 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setImage:newImage4 forState:UIControlStateNormal];
button.frame = CGRectMake(20.0, 50.0, 280, 255.0);
/////////////////////////////////// 第二种方式:
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setTitle:@"photo.png" forState:UIControlStateNormal];
[[button2 titleLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
[button2 setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button2 setBackgroundColor:[UIColor blackColor]];
UIImage *btnImage = [UIImage imageWithData:"png image from url"];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:btnImage];
imageView2.frame = CGRectMake(0, 0, 280, 209);
button2.frame = CGRectMake(20.0, 50.0, 280, 255.0);
[button2 addSubview:imageView2];