2012-03-26 18 views
0

Ich versuche, UIImagages vom Server asynchron in UITableViewCell zu laden. Mein Code funktionierte im Simulator, aber nicht auf dem Gerät. Mein Code wie folgt,UImage funktioniert nicht richtig?

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
IScopeCustomTableCell *cell = (IScopeCustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName]; 

if (!cell){ 
    NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil]; 
    cell = [topLevelItems objectAtIndex:0]; 
} 

cell.delegate = self; 
cell.videoTitle.text = [[videoDataArray objectAtIndex:indexPath.row] objectForKey:@"VideoTitle"]; 
cell.videoLink = [[videoDataArray objectAtIndex:indexPath.row] objectForKey:@"VideoLink"]; 
cell.videoThumbnailImageLink = [[videoDataArray objectAtIndex:indexPath.row] objectForKey:@"VideoThumbnail"]; 
cell.videoThumbnail.tag = indexPath.row; 
cell.tag = indexPath.row; 
[cell.activityIndicator startAnimating]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
            initWithTarget:self 
            selector:@selector(loadImage:) 
            object:cell]; 
[queue addOperation:operation]; 
return cell; 
} 

- (void)loadImage:(IScopeCustomTableCell *)cell { 
NSLog(@"Image link :- %@", cell.videoThumbnailImageLink); 

//NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:cell.videoThumbnailImageLink]]; 
NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cell.videoThumbnailImageLink]]; 
UIImage* image = [UIImage imageWithData:imageData]; 

cell.videoThumbnail.image = image; 
[cell.activityIndicator stopAnimating]; 
[cell.activityIndicator removeFromSuperview]; 

//[self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO]; 
} 

Der obige Code Geldbusse Simulator, aber nicht auf dem Gerät, becoz, UIImage * Bild get (0X0) null in obwohl NSData loadimage Verfahren geeignete Daten enthalten.

+0

howmany mb der Dateigröße vom Server kommen ??? – parag

+0

@paragkavar, 20KB – Tirth

+0

Entschuldigung für die Angabe der offensichtlichen, aber Sie haben bereits überprüft, dass Ihr Gerät Zugriff auf das Internet hat, nicht wahr? Außerdem ist das Abfeuern eines Threads bei jeder Anzeige der Zelle nicht die beste Lösung. Sie können viele Tutorials im Web finden, um Bilder asynchron zu laden. – Leonardo

Antwort

0

Dieser Link i enthält, mage cache code und es funktionierte genau genau das würde ich mir wünschen.

https://github.com/jakemarsh/JMImageCache

+0

http://maniacdev.com/2012/04/best-resources-in-ios-development- 2. April 2012 /? utm_source = feedburner & utm_medium = füttern & utm_campaign = Feed% 3A + maniacdev +% 28iPhone% 2C + iOS + 4% 2C + iPad + SDK + Entwicklung + Tutorial + und + Programmierung + Tipps% 29 – Tirth

+0

http: // gaeit .ro/tag/cocos2d-x / – Tirth

0

Was Sie tun, ist nicht der beste Weg, um es zu tun, da es viele Autoreleased Objekte erstellen wird und die Größe Ihrer App zu erhöhen und Sie nicht Ihre operation freigeben ... so zunächst Ihre operation nach [queue addOperation:operation];

und diesen Code verwenden, anstatt die Bilddaten für das Abrufen und es in Ihrem Bild zu speichern ...

NSData* data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:cell.videoThumbnailImageLink]]]; 
UIImage* img = [[UIImage alloc] initWithData:data]; 
[data release]; 
cell.videoThumbnail.image = image; 
[img release]; 

der Hoffnung, diese sortiert Ihr Problem ..

+0

mein Freund habe ich schon versucht, aber nicht für mich gearbeitet .... – Tirth