0

正如标题所说,我正在尝试使用这样的二维码 - 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];
4

1 回答 1

0

尝试像这样对 url 进行编码,然后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];
于 2012-06-24T19:45:39.117 回答