Xcode - Adding UIImages to array from RSS feed URL issue
"-[NSMutableArray insertObject:atIndex:]: attempt to开发者_如何学运维 insert nil object at 0'"
If I use a hard-coded path ('path' in the code) it adds the image correctly. when I retrieve the same URL from an RSS feed, it doesn't work (error above).
I tried checking the URL with a simple equality check and it indicates they are not equal, though they appear to be so in the debugger output.
Any insight into memory management would also be helpful. I'm coming from the Flash work world and a noob to xcode (sorry). I know I need to stop thinking of memory management as an afterthought...
--
- (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];
}
A friend helped me find the answer. The RSS feed was including newlines which I didn't catch in the debugger output. Cleaned them up with:
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];
}
}
精彩评论