2011-05-14 64 views
0

阵 “ - [NSMutableArray的insertObject:atIndex:]:尝试在0插入无对象”Xcode中 - 添加UIImages从RSS提要URL问题

如果我使用硬编码路径( '路径'代码)它正确地添加图像。当我从RSS提要检索相同的URL时,它不起作用(上面的错误)。

我试着用一个简单的等式检查来检查URL,它表明它们不相等,虽然它们在调试器输出中看起来如此。

任何洞察内存管理也将是有帮助的。我来自Flash的工作世界和noob到xcode(抱歉)。我知道我需要停止内存管理思想作为一种事后...

-

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self prepareGalleryLoad]; //sets up coverflow, waiting for data 

    NSString *xmlpath =  @"http://mydomain.com/gallery3/index.php/rss/feed/gallery/album/12"; 

xmlreader = [[XMLReader alloc] init]; 
[xmlreader parseXMLFileAtURL:xmlpath]; 


NSMutableArray *imagearray = [[NSMutableArray alloc]init]; //array of images 
NSMutableArray *imagearraycaptions = [[NSMutableArray alloc]init]; //array of captions for images 

NSString *path; 
NSString *pathfromrss; 

UIImage *tempimage; 

for (int y = 0; y < [xmlreader.stories count]; y++){ 
    [imagearraycaptions addObject:[[xmlreader.stories objectAtIndex:y] objectForKey:@"title"]]; 
    path = @"http://mydomain.com/gallery3/var/resizes/College-of-Nursing-and-Health-Studies/health10.jpg?m=1299713266";  
    pathfromrss = [NSString stringWithFormat:@"%@",[[xmlreader.stories objectAtIndex:y] objectForKey:@"link"]]; 

    //DIAGNOSTIC check of path vs path temp. 
    if (path == pathfromrss){ 
     NSLog(@"path is equal"); 
    }else{ 
     NSLog(@"path is not equal"); 
     NSLog(@"%@",path); 
     NSLog(@"%@",pathfromrss); 
    } 

    //END DIAGNOSTIC path check 

    tempimage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:path]]]; 

    //tempimage is NIL when using "pathfromrss", works fine with "path" 
    if(tempimage){ 
     [imagearray addObject:tempimage]; 
    } 
} 

coverstext = imagearraycaptions; //coverflow uses this 
covers = imagearray; //coverflow uses this 

[self finishGalleryLoad]; 

}

回答

0

一个朋友帮我找到了答案。 RSS提要包含了我在调试器输出中没有看到的换行符。清理它们:

for (int y = 0; y < [xmlreader.stories count]; y++){ 
    pathfromrss = [NSString stringWithFormat:@"%@",[[xmlreader.stories objectAtIndex:y] objectForKey:@"link"]]; 
    [coverflowdescription setText:pathfromrss]; 

    NSString *cleanurl = [pathfromrss stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:cleanurl]]; 

    UIImage *tempimage = [[UIImage alloc] initWithData:mydata]; 
    if(tempimage){ 
     [imagearraycaptions addObject:[[xmlreader.stories objectAtIndex:y] objectForKey:@"title"]]; 
     [imagearray addObject:tempimage]; 
    } 
}