0

タイトルにあるように、 http: //chart.apis.google.com/chart?cht=qr&chs=200x200&chl=044779-A55W6UZD&chld=H|0 のような qr コードを取り、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 に答える