2016-07-14 8 views
2

私はUITableViewCellを持っています。この画像をロードしていますので、画像にAsynchroniesとshow activate indicatorをロードします。今回は、イメージの読み込み時間が非常に長く、イメージがテーブルビューに表示された後にすべてのイメージが読み込まれます。何度も画像が読み込まれず、ユーザーUIInterfaceがハングします。私は非常に疲れました、私はSDWebImageライブラリを使用しました。どのようにお手伝いしてください、ありがとうございますSDWebImageの使い方

#import "UIImageView+WebCache.h" 

@interface pictureViewController() 
{ 
    NSArray*picarray; 
    NSArray*titlearray; 
    NSArray*idarray; 
} 

@end 

@implementation pictureViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}]; 

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"background2.png"] forBarMetrics:UIBarMetricsDefault]; 

    _tableview.separatorColor = [UIColor yellowColor]; 

    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_gallery.php"]]; 
    response =[[NSMutableData alloc]init]; 
    [NSURLConnection connectionWithRequest:req delegate:self]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [response appendData:data]; 
    NSLog(@"error receving data %@",response); 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSError *error; 

    //NSLog(@"Error in receiving data %@",error); 
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; 
    // NSLog(@"response data %@",json); 

     NSArray *results = [json objectForKey:@"status"]; 
     picarray = [results valueForKey:@"img_url"]; 
     titlearray = [results valueForKey:@"title"]; 
     // NSLog(@"my title is%@",titlearray); 
     idarray=[results valueForKey:@"id"]; 
     //NSLog(@"my galary id is%@",idarray); 
     [self.tableview reloadData]; 
} 


// Do any additional setup after loading the view, typically from a nib. 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [picarray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // NSLog(@"tableview cell"); 
    picture_viewcontrollerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htr"]; 
    if (cell==nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    //NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [picarray objectAtIndex:indexPath.row]]]; 
    // UIImage* image = [[UIImage alloc] initWithData:imageData]; 
    //cell.picture.image =image; 

    cell.titlelbl.text=[NSString stringWithFormat:@"%@",[titlearray objectAtIndex:indexPath.row]]; 

    NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"]; 
    [cell.picture setContentMode:UIViewContentModeScaleAspectFill]; 
    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]]; 


    return cell; 
} 

@end 

答えて

2

あなたはこの(SDWebImageを使用して表示activityIndi​​cator &ロードイメージを)試してみてください -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // NSLog(@"tableview cell"); 
    picture_viewcontrollerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htr"]; 
    if (cell==nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.titlelbl.text=[NSString stringWithFormat:@"%@",[titlearray objectAtIndex:indexPath.row]]; 

    NSURL*url= [NSURL URLWithString:[NSString stringWithFormat:@"%@",[picarray objectAtIndex:indexPath.row]]]; 

    [cell.picture setContentMode:UIViewContentModeScaleAspectFill]; 

    [cell.picture setShowActivityIndicatorView:YES]; 

    [cell.picture setIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder.png"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; 

    return cell; 

} 
+0

'[<__ NSCFString 0x7aff3ea0> valueForUndefinedKey:]:このクラスは、キーimg_urlのキーコードに準拠していません。' –

+0

この行を変更する - NSURL * url = [NSURL URLWithString:[[picarray objectAtIndex:indexPath.row] valueForKey:@ "img_url"]]; –

+0

'[<__ NSCFString 0x79f168a0> valueForUndefinedKey:]:このクラスは、キーimg_urlのキーコードに準拠していません。'というエラーメッセージが表示されます。 同じエラーが表示されます –

0

あなたのコードはコンパイルできますか?あなたのcellForRowAtIndexPathでは、あなたがimageDataの宣言をコメントアウトしたときに[cell.picture sd_setImageWithURL:imageData placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]]; と表示されます。

あなたはコメントしなかった場合でも、この行は、あなたが代わりに、直接URLを渡す必要が

NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"]; 
[cell.picture setContentMode:UIViewContentModeScaleAspectFill]; 
[cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]]; 

代わりの

NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"]; 
[cell.picture setContentMode:UIViewContentModeScaleAspectFill]; 
[cell.picture sd_setImageWithURL:imageData placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]]; 

を使用する必要が

[cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]]; 
+0

申し訳ありませんが、私のコードを編集します。私の質問をもう一度見てください –

0

する必要がありますimageDataの一覧

+0

私のコードを編集してくださいcheckしてください –

+0

@ A.Kumawatはい、うまく動作している可能性があります。 –

0

このコードを試してください

  1. UIActivityIndicatorViewをtableviewセルに追加してください。
  2. cellForRowAtIndexpathのコードが変更されました。

    cell.activityIndicator.center = imageView.center; 
    cell.activityIndicator.hidesWhenStopped = YES; 
    NSURL *url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"]; 
    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
             [cell.activityIndicator stopAnimating]; 
             }]; 
    [cell.picture addSubview:activityIndicator]; 
    [cell.activityIndicator startAnimating]; 
    
+0

UITableViewCellクラス "activityIndi​​cator"で宣言されていますか? –

+0

@ A.Kumawatはい、UITableViewCellに追加します。 –

+0

最後の行[activityIndi​​cator startAnimating];表示されていない –