UIImageView图像没有返回nil但图像没有显示

时间:2014-12-19 02:23:51

标签: uiimageview uiimage nsurl nlog null

我有一个图像视图,它从互联网上提取网址这是我从RSS Feed获取的网址 -  它是从rss feed .xml中提取的图像,这是我用来获取图像的代码。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *URL = [NSURL URLWithString:@"www.something.com/rss/hi.xml"];
    NSData *data = [NSData dataWithContentsOfURL:URL];

    // Assuming data is in UTF8.
    NSString *string = [NSString stringWithUTF8String:[data bytes]];




    NSString *webStringz = string;




    NSString *mastaString;

    mastaString = webStringz;



    NSScanner *theScanner2;
    NSString *imageURL2;

    theScanner2 = [NSScanner scannerWithString: mastaString];

    // find start of tag
    [theScanner2 scanUpToString: @"<media:content url=\"" intoString: nil];
    if ([theScanner2 isAtEnd] == NO) {

        // find end of tag
        [theScanner2 scanUpToString: @"\" " intoString: &imageURL2];
        imageURL2 = [imageURL2 stringByReplacingOccurrencesOfString:@"<media:content url=\"" withString:@""];

        imageURL2 = [imageURL2 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        imageURL2 = [imageURL2 stringByReplacingOccurrencesOfString:@" " withString:@""];
                //Download Image
        NSURL * imageURL = [NSURL URLWithString:[imageURL2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]])];

        UIImage * image = [UIImage imageWithData:imageData];

        [imageView setImage:image];

        NSLog(@"Image URL #1: %@", imageURL2);
        mastaString = [mastaString stringByReplacingOccurrencesOfString:imageURL2 withString:@""];

        if (imageView.image == nil) {
            NSLog(@"IS NIL");

        }
        else if (image == nil) {
            NSLog(@"IS NIL");

        }


    // Do any additional setup after loading the view.
}

}

在我的NSLOG中,它说图像网址及其有效。 它没有显示IS NIL 它没有显示IS NIL 2

但ImageView没有显示图像?为什么?它全部联系起来了吗?

由于

1 个答案:

答案 0 :(得分:0)

首次访问UIViewController视图时,必须没有创建图像视图。请注意,如果它是nil,则self.view将触发viewDidLoad方法,但是imageView为nil。所以你最好在viewDidLoad方法中创建imageView。