Hi Readers,
This blog includes the concept of converting NSData to UIImage with the help of a very simple example. Given below is the screenshot and code which is used to convert NSData to UIImage on button click. Snapshot of storyboard is given below:-
Code for the same is given below:-
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
- (IBAction)btnConvert:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "StringDeclaration.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnConvert:(id)sender {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"txt"];
NSError *error;
NSString *myText = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:&error];
NSData *data = [[NSData alloc] initWithBase64EncodedString:myText options:NSDataBase64DecodingIgnoreUnknownCharacters]; //
UIImage *convertImageFromNSData = [UIImage imageWithData:data];
_imgView.image=convertImageFromNSData;
}
@end
Output:-
You can also download the project from the link given below:-
0 Comment(s)