0
我有一個圖像視圖,它從互聯網上拉的URL這是我從RSS源獲取的網址 - 它的圖像從rss飼料拉取.xml
這裏是我使用的代碼獲取圖像。UIImageView圖像不返回零,但圖像不顯示
- (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它說,圖像URL和有效。 它沒有顯示IS NIL 它沒有顯示IS NIL 2
但ImageView沒有顯示圖像?爲什麼?它的所有鏈接和一切?
感謝
您是否將imageView添加到視圖層次結構中? – KudoCC 2014-12-19 02:35:50
我不知道這意味着什麼對不起,我很新。 – 2014-12-19 02:40:47
那麼,如果您沒有將圖像視圖添加到根視圖(在大多數情況下它是視圖控制器的視圖),則儘管它已分配,但無法看到它。 – KudoCC 2014-12-19 02:44:19