0

我有两种将图像添加到表格的方法,哪种方法最有效,还有更好的方法吗?两种方式都放置在带有“图层对象”的按钮顶部。

另外我注意到表格在模拟器中滚动缓慢,这种缓慢滚动可能发生在真手机上吗?我看到原因的唯一原因可能是因为我没有关联按钮,但它可能与记忆有关,或者在每个单元格中绘图。我不能是因为图像来自 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];
4

1 回答 1

1

这里您的一些图像来自远程服务器。您应该使用延迟加载从远程服务器加载图像。它将异步下载图像并根据需要加载图像。您还可以使用SDWebImage库来执行此操作并且还具有缓存机制。使用这个库非常简单直接。像:

 [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
于 2012-07-01T17:20:41.130 回答