1

当我为每个单元格设置图像时,我的应用程序滚动和搜索速度非常慢。图片提升速度更快,但搜索时仍然很慢。这是我在行单元格中的代码。有任何想法吗?

@interface UIImage (TPAdditions)
- (UIImage*)imageScaledToSize:(CGSize)size;
@end

@implementation UIImage (TPAdditions)
- (UIImage*)imageScaledToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSInteger buton1_tag = 100;
    static NSInteger buton2_tag = 101;

    if (indexPath.section == 0) {
        CellIdentifier = @"CellMuzikEkle";
    }else{
        if(indexPath.row == 0 && !self.editing)
            CellIdentifier = @"CellPlayereEkle";
        else
            CellIdentifier = @"CellDizi";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        if (indexPath.section == 1 && indexPath.row == 0 && !self.editing) // Grubu ekle
            cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell4" owner:self options:nil] objectAtIndex:0];
        else{
            if (indexPath.section == 0) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil] objectAtIndex:5];
                UIButton *ipodbuton = (UIButton *) [cell.contentView viewWithTag:buton1_tag];
                [ipodbuton addTarget:self action:@selector(iPoddan_Ekle:) forControlEvents:UIControlEventTouchUpInside];
                UIButton *yuklenenler = (UIButton *) [cell.contentView viewWithTag:buton2_tag];
                [yuklenenler addTarget:self action:@selector(Yuklenenlerden_Ekle:) forControlEvents:UIControlEventTouchUpInside];
            }else{
                cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
                cell.textLabel.numberOfLines=2;
                cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
                [cell.imageView.layer setBorderWidth: 1.5];
            }
        }
    }

    if (indexPath.section == 1 && !self.editing && indexPath.row != 0)
        cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
        else
            cell.accessoryType =UITableViewCellAccessoryNone;


            if(indexPath.section == 0){
                cell.textLabel.text = @"";
                cell.detailTextLabel.text = @"";
                cell.imageView.image = nil;
            }else{
                if (indexPath.row > 0 || self.editing) {
                    if (!self.editing)
                        indexPath = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section];

                    NSArray *aradizi = tableView == self.searchDisplayController.searchResultsTableView ? [[NSArray alloc] initWithArray:self.filteredListContent] : [[NSArray alloc] initWithArray:self.dizi];

                    if ([[NSUserDefaults standardUserDefaults] boolForKey:cellresimyuk_s])
                        cell.imageView.image = [[self Resim_Artwork:[aradizi objectAtIndex:indexPath.row]] imageScaledToSize:CGSizeMake(65, 50)];
                    else
                        cell.imageView.image = nil;

                    cell.textLabel.text = [[aradizi objectAtIndex:indexPath.row] objectForKey:isim_s];

                    NSTimeInterval theTimeInterval = [[[aradizi objectAtIndex:indexPath.row] objectForKey:sure_s] intValue] - 1;
                    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
                    NSDate  *date1;
                    if (theTimeInterval >= 3600) {
                        [dateFormatter setDateFormat:@"HH:mm:ss"];
                        date1   = [dateFormatter dateFromString:@"00:00:00"];
                    }else{
                        [dateFormatter setDateFormat:@"mm:ss"];
                        date1   = [dateFormatter dateFromString:@"00:00"];
                    }
                    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
                    cell.detailTextLabel.text = [dateFormatter stringFromDate:date2];
                }else{
                    if (tableView == self.searchDisplayController.searchResultsTableView)
                        cell.textLabel.text = AramaSonuclrPlayerEkle_lclz;
                    else
                        cell.textLabel.text = GrubuPlayereEkle_lclz;

                }
            }


    return cell;
}

Resim_Artwork

- (UIImage *)Resim_Artwork:(NSDictionary *)dictr{


    if ([dictr objectForKey:videoid_s]) {
        if ([self Dosya_Varmi:[[rsm_favori stringByAppendingPathComponent:[dictr objectForKey:videoid_s]] stringByAppendingPathExtension:@"png"]])
            return [UIImage imageWithContentsOfFile:[[rsm_favori stringByAppendingPathComponent:[dictr objectForKey:videoid_s]] stringByAppendingPathExtension:@"png"]];
    }else{
        if ([[[dictr objectForKey:urlsi_s] pathExtension] isEqualToString:@"mp4"]) {
            AVURLAsset *assetresim = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[kDocdir stringByAppendingPathComponent:[dictr objectForKey:urlsi_s]]] options:nil];
            AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:assetresim];
            gen.appliesPreferredTrackTransform = YES;
            int suresi = CMTimeGetSeconds(assetresim.duration);
            if (suresi > 0) {
                CMTime time;
                if (suresi>6)
                    time = CMTimeMakeWithSeconds(6.0, 600);
                else
                    time = CMTimeMakeWithSeconds(0.0, 600);
                NSError *error = nil;
                CMTime actualTime;
                CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
                UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
                CGImageRelease(image);
                return thumb;
            }
        }else {
            if ([dictr objectForKey:kaynak_s]) {
                AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[dictr objectForKey:urlsi_s]] options:nil];
                for ( AVMetadataItem* item in [asset commonMetadata] ) {
                    if ([[item commonKey] isEqualToString:@"artwork"] )
                        if (item.dataValue != nil)
                            if ([UIImage imageWithData:item.dataValue])
                                return [UIImage imageWithData:item.dataValue];
                }
            }else{
                if ([self Dosya_Varmi:[kDocdir stringByAppendingPathComponent:[dictr objectForKey:urlsi_s]]]){
                    AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[kDocdir stringByAppendingPathComponent:[dictr objectForKey:urlsi_s]]] options:nil];
                    for ( AVMetadataItem* item in [asset commonMetadata] ) {
                        if ([[item commonKey] isEqualToString:@"artwork"] )
                            if (item.dataValue != nil)
                                if ([UIImage imageWithData:item.dataValue])
                                    return [UIImage imageWithData:item.dataValue];
                    }

                }
            }
        }
    }


    return [self Resimm:@".varsayilan" Koordinat:CGRectMake(120,1320, 80, 60) retinami:0 grubu:6];

}

tableView willDisplayCell

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *viewControllers = [self.tabBarController viewControllers];
    NSArray *viewControllers2 = [[viewControllers objectAtIndex:0] viewControllers];
    Player *detailViewController = (Player *)[viewControllers2 objectAtIndex:0];

    [detailViewController.temalar Cell_Tema:cell];

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    cell.textLabel.textColor = [detailViewController.temalar Baslik_Renk];
    cell.detailTextLabel.textColor = [detailViewController.temalar Bilgi_Renk];
    [cell.imageView.layer setBorderColor: [tableView.separatorColor CGColor]];


    [((UIButton *) [cell.contentView viewWithTag:100])    setBackgroundImage:[self Resimm:@".buton" Koordinat:CGRectMake(0,1350, 94, 35) retinami:0 grubu:7] forState:UIControlStateNormal];
    [((UIButton *) [cell.contentView viewWithTag:100]) setTitleColor:[detailViewController.temalar Baslik_Renk] forState:UIControlStateNormal];
    [((UIButton *) [cell.contentView viewWithTag:101])    setBackgroundImage:[self Resimm:@".buton" Koordinat:CGRectMake(0,1350, 94, 35) retinami:0 grubu:7] forState:UIControlStateNormal];
    [((UIButton *) [cell.contentView viewWithTag:101]) setTitleColor:[detailViewController.temalar Baslik_Renk] forState:UIControlStateNormal];
}
4

2 回答 2

1

这里:

   NSArray *aradizi = tableView == self.searchDisplayController.searchResultsTableView ? 
   [[NSArray alloc] initWithArray:self.filteredListContent] : 
   [[NSArray alloc] initWithArray:self.dizi];

您应该尽可能避免在 cellForRowAtIndexPath 内创建对象。
无论如何,您不需要:

   NSArray *aradizi = tableView == self.searchDisplayController.searchResultsTableView ? 
   self.filteredListContent : 
   self.dizi;

在这里,您似乎在动态创建缩略图并调整其大小。:

   if ([[NSUserDefaults standardUserDefaults] boolForKey:cellresimyuk_s])
       cell.imageView.image = [[self Resim_Artwork:[aradizi objectAtIndex:indexPath.row]] 
       imageScaledToSize:CGSizeMake(65, 50)];

这势必会阻塞流畅的tableView scolling。所有这些都应该被预处理和缓存,或者至少在这里只处理一次并缓存以供重用。

像这样耗时的非 UI 处理可以转到另一个线程,这样即使图像还没有准备好显示,你的 scolling 也能保持流畅。像……</p>

      if ([[NSUserDefaults standardUserDefaults] boolForKey:cellresimyuk_s])
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
             UIImage* image = 
                [[self Resim_Artwork:[aradizi objectAtIndex:indexPath.row]] 
                    imageScaledToSize:CGSizeMake(65, 50)];
             dispatch_async(dispatch_get_main_queue(), ^{
                 //check our cell is still valid
                 if ([[self.tableView cellForRowAtIndexPath:indexPath] isEqual:cell]){
                     cell.imageView.image = image;
                     }
             });
         });

        else
            cell.imageView.image = nil;

关于日期格式化程序:这些是重量级对象,您需要确保只创建一次,保存在一个属性中以供重用。我建议您执行以下操作:

声明两个日期格式化程序属性和另一个作为您的归零日期。在 viewDidLoad 中初始化它们:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.hoursFormat = [[NSDateFormatter alloc] init];
        [self.hoursFormat setDateFormat:@"HH:mm:ss"];
        self.minsFormat =  [[NSDateFormatter alloc]init];
        [self.minsFormat setDateFormat:@"mm:ss"];
        self.zeroDate   = [self.hoursFormat dateFromString:@"00:00:00"];
    }

然后在 cellForRowAtIndexPath 中:

     NSDate* date = [self.zeroDate dateByAddingTimeInterval:theTimeInterval];
        if (theTimeInterval >= 3600) {
            self.label.text = [self.hoursFormat stringFromDate:date];
        } else {
            self.label.text = [self.minsFormat stringFromDate:date];
        }
于 2013-01-27T15:47:12.620 回答
0

他是,谢谢你的回答。表现非常好。但自从我做了这个改变,程序就关闭了。新代码:

NSMutableArray *aradizi;
if (tableView == self.searchDisplayController.searchResultsTableView)
    aradizi = self.filteredListContent;
if (indexPath.section == 2 && tableView != self.searchDisplayController.searchResultsTableView)
    aradizi = self.dizi_klasorler;
if (indexPath.section == 3)
    aradizi = self.dizi_veriler;



if ([[NSUserDefaults standardUserDefaults] boolForKey:cellresimyuk_s]){
    if (cell.imageView.image == nil)
        cell.imageView.image = [[self Resimm:@".varsayilan" Koordinat:CGRectMake(120,1320, 80, 60) retinami:0 grubu:6] Resim_Skala:CGSizeMake(60, 45)];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage* image = [[self Resim_Artwork:[[aradizi objectAtIndex:indexPath.row] objectForKey:urlsi_s]] Resim_Skala:CGSizeMake(60, 45)];
        dispatch_async(dispatch_get_main_queue(), ^{
            //check our cell is still valid
            if (tableView == self.searchDisplayController.searchResultsTableView){
                if ([[self.searchDisplayController.searchResultsTableView cellForRowAtIndexPath:indexPath] isEqual:cell])
                    cell.imageView.image = image;
            }else{
                if ([[self.tableView cellForRowAtIndexPath:indexPath] isEqual:cell])
                    cell.imageView.image = image;
            }
        });
    });
}else
    cell.imageView.image = nil;

http://img29.imageshack.us/img29/1083/ekranresmi2013020301553.png

http://img502.imageshack.us/img502/5591/ekranresmi2013012919525.png

于 2013-02-03T00:14:49.780 回答