How to display a base64 image within a UIImageView?

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

I got this Base64 gif image:

R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmN         


        
11条回答
  •  失恋的感觉
    2020-11-30 01:30

    Code snippet for downloading a text file with base64 image string inside, then saving/reading the file and finally creating an image to display right in the view

    In Case You Need To Decode Image To Base64 Click Here

    NSString *downloadUrl = "http://www.server.com/file.txt";
    //
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: downloadUrl]];
    //
    // DOWNLOAD THE FILE
    //
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if (error) {
    
            NSLog(@"Download Error:%@",error.description);
    
        }
        if (data) {
            [data writeToFile:filePath atomically:YES];
            //
            // PROCESS BASE 64 STRING
            //
            NSString * fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
            //
            // PROCESS BASE 64 STRING
            //
            NSString *base64String = fileContents;
            NSURL *url = [NSURL URLWithString:base64String];
            NSData *imageData = [NSData dataWithContentsOfURL:url];
            //
            // CREATE IMAGE
            //
            UIImage *ret = [UIImage imageWithData:imageData];
            self.myImage.image = ret;
        }
    }
    

提交回复
热议问题