Nothing displayed after splitting a list of image URLs

a 夏天 提交于 2019-12-12 01:12:28

问题


I have six image URLs in a string named aux. I split it with component separated by newlines. I want to pick images one by one from aux and display each in an ImageView. My code is:

aux = [_homeText.text stringByAppendingString:[NSString stringWithFormat:@" %@  ",item.shortDescription]];
[_homeText setText:aux];  
NSString *list =aux;
NSArray *listItems = [list componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
[listItems count];
if ([listItems objectAtIndex:0]) {
        NSURL *url = [NSURL URLWithString:aux];
            /*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hai" message:aux delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alert show];
                [alert release];*/          
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc] initWithData:data]; 
        [_homeImage setImage:image];
}
else if ([listItems objectAtIndex:1])
{
            NSURL *url = [NSURL URLWithString:aux];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [[UIImage alloc] initWithData:data]; 
            [_homeImage setImage:image];
}

There is no error but I get no image. Why? How do I fix this?


回答1:


If both data and image not nil, you may try to correctly initialize _homeImage. I don'w know why, but I think what you created _homeImage like this:

_homeImage = [[UIImageView alloc] init];

Try to do something like this:

CGSize imageSize = [image size];
[_homeImage setFrame:CGRectMake([_homeImage frame].origin.x, 
                                [_homeImage frame].origin.y, 
                                imageSize.width, imageSize.height)];
[_homeImage setImage:image];
[image release], image = nil;


来源:https://stackoverflow.com/questions/6952229/nothing-displayed-after-splitting-a-list-of-image-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!