How to display a base64 image within a UIImageView?

后端 未结 11 2201
攒了一身酷
攒了一身酷 2020-11-30 00:56

I got this Base64 gif image:

R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmN         


        
11条回答
  •  猫巷女王i
    2020-11-30 01:47

    You don't have to encode it. Simply make a NSUrl, it knows the "data:"-url.

    NSURL *url = [NSURL URLWithString:base64String];    
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    UIImage *ret = [UIImage imageWithData:imageData];
    

    As mentioned in the comments, you have to make sure that you prepend your data with data:image/png;base64, or else your base64 data is useless.

提交回复
热议问题