我正在尝试使用从Web服务获取的图像数据填充UIImageView。以下是我想要做的事情,它根本没有填充图像。加载视图时,UIImageView为空白。这是在viewDidLoad方法中。
imageData=@"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsK\nCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQU\nFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAHgAoADASIA\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD4I/aM\n/wCSyeIf+3f/ANJ4682r0n9oz/ksniH/ALd//SeOvNqACiiigAooooAKKKKAPtr9nL/kjXh7/t4/\n9KZa9Jrzb9nL/kjXh7/t4/8ASmWvSaACiiigAooooAKKKK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16J/4MYf8A4qvgGigD7a/Zy/5I14e/7eP/\nAEplr0mvNv2cv+SNeHv+3j/0plr0mgAooooAKKKKACiiigD4l/aM/wCSyeIf+3f/ANJ4682r0n9o\nz/ksniH/ALd//SeOvNqACiiigAooooAKKKKAPtr9nL/kjXh7/t4/9KZa9Jrzb9nL/kjXh7/t4/8A\nSmWvSaACiiigAooooAKKKKAPiX9oz/ksniH/ALd//SeOvNq9J/aM/wCSyeIf+3f/ANJ4682oAKKK\nKACiiigAooooA+2v2cv+SNeHv+3j/wBKZa9Jrzb9nL/kjXh7/t4/9KZa9JoAKKKKACiiigAooooA\n+Jf2jP8AksniH/t3/wDSeOvNq9J/aM/5LJ4h/wC3f/0njrzagAooooAKKKKACiiigD7a/Zy/5I14\ne/7eP/SmWvSa82/Zy/5I14e/7eP/AEplr0mgAooooAKKKKACiiigD4l/aM/5LJ4h/wC3f/0njrza\nvSf2jP8AksniH/t3/wDSeOvNqACiiigAooooAKKKKAPtr9nL/kjXh7/t4/8ASmWvSa82/Zy/5I14\ne/7eP/SmWvSaACiiigAooooAKKKKAP";
NSString * temp=[NSString stringWithFormat:@"data:image/png;base64,%@",imageData];
NSURL *url = [NSURL URLWithString:temp];
NSData *d = [NSData dataWithContentsOfURL:url];
UIImage *ret = [UIImage imageWithData:d];
[Photo setImage:ret];
Photo是一个UIImageView。有什么建议可以帮助我吗?
答案 0 :(得分:1)
我能够使用如下方法
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
并从webservice调用中传递我的base64字符串,如下所示:
UIImage *image = [self decodeBase64ToImage:imageData];
[Photo setImage:image];