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_アートワーク

- (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 スクロールを妨害することになります。これらはすべて前処理してキャッシュするか、少なくともここで一度だけ処理して再利用のためにキャッシュする必要があります。

このような時間のかかる非 UI 処理は別のスレッドに進むことができるため、画像を表示する準備ができていなくても、スクロールはスムーズに保たれます。みたいな…</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;

日付フォーマッタについて: これらは重いオブジェクトであるため、一度だけ作成し、再利用のためにプロパティに保持する必要があります。次のことをお勧めします。

2 つの日付フォーマッタ プロパティと、ゼロの日付用に別のプロパティを宣言します。それらをすべて 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 に答える