从url加载qr代码并显示为图像视图

时间:2012-06-24 19:34:39

标签: iphone objective-c ios xcode

正如标题所说,我正在尝试使用这样的qr代码 - http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=044779-A55W6UZD&chld=H|0并将其显示为UIImageview。我搜索过但无法在任何地方找到答案。请帮忙。 我试过这个,但似乎没有用

    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];

1 个答案:

答案 0 :(得分: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];
相关问题