2012-06-24 84 views
0

正如標題所示,我正嘗試拍攝如此的qr碼 - http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=044779-A55W6UZD&chld=H|0並將其顯示爲UIImageview。我已經搜索,但無法找到任何答案。請幫忙。 我已經試過這一點,但它似乎沒有工作從url加載qr碼並顯示爲圖像視圖

NSData *receivedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; 
UIImage *image = [[UIImage alloc] initWithData:receivedData]; 
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image]; 
myImageView.frame = CGRectMake(20, 160, 200, 200); 
[self.view addSubview:myImageView]; 
+0

什麼不起作用?你用哪種方法編寫這段代碼? – Andreas

+0

URL不會加載到NSData * recievedData中。如果我在NSData * recievedData之後放置一個NSLog,它說它爲空。 – Sean

回答

0

編碼嘗試這樣的網址,然後在代碼的其餘部分使用encodedUrl

NSString* encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

你應該還記得釋放你創建的對象,否則你會泄漏內存。

NSData* receivedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:encodedUrl]]; 
UIImage* image = [[UIImage alloc] initWithData:receivedData]; 
UIImageView* myImageView = [[UIImageView alloc] initWithImage:image]; 
myImageView.frame = CGRectMake(20, 160, image.size.width, image.size.height; 
[self.view addSubview:myImageView]; 

[receivedData release]; 
[image release]; 
[myImageView release]; 
+0

非常感謝! – Sean

+0

不客氣:) – Andreas