Home / iPhone

Exploring the Foundation Framework in in iPhone - NS Data Class

Rating: 4
Views: 1866
by Ravindra Savaram
Last modified: March 25th 2021

NSData and its mutable subclass NSMutableData provide data objects, object-oriented wrappers for byte buffers. Data objects let simply allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation Objects.

NSData creates static Data Objects, and NSMutableData creates dynamic data objects. NSData and NSMutableData are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications.

NSData Class

The size of the data is subject to a theoretical limit of about 8 ExaBytes (in practice, the limit should not be a factor).

If you want to enrich your career and become a professional in IOS, then Enrol Our "IOS Development Training" This course will help you to achieve excellence in this domain.

      UIImage *IMG  =[UIImage imageNamed:@  “1.jpg”];
      NSLog (@  “img is %@”,img);

[ Related Article: What is Swift ]

// How to convert image object to data:

NSData*data =  UIImage JPEG Representation (img,1);
NSLog(@  “ image  data is %@”,data);

// Converting the png Image object to data:

NSData  *data2  =  UIImage PNGRepresentation  (img);
NSLog (@ “ png Image data is %@”, data2);

[ Related Article: IOS Development Interview Questions & Answers ]

//How to know the size of an image:

Int size = [data length];
If (size /1024>0)
  NSLog (@  “ image size is %d  kb”,  size/1024);
else
NSLog (@ “image size is %d Bytes , size);

//How to convert data to image object:

UIImage *img2 = [UIImage ImageWithData : data];
NSLog(@ “the image is %@”,img2);

//How to Append Data:

NSMutableData*mutData= [[NSMutableData alloc]init];
[MutData appendData : data];
NSLog (@ “the mutable data is %@”, mutData);

//Getting URL image as Data:

NSData *URL  data = [NSData  datawithContentsOfURL :
[NSURL URLwithString:@https://www.Cnet.co.uk/i/c/blg/cat/mobiles/ios5.jpg”]];
UIImage   *URLImage   =[UIImage ImageWithData : URL data];
NSLog (@ “data length is %d kb ,[URL data length]1024];

//Setting the image as a background image for view:

self view background color=[UIColor Color with PatternImage: URLImage];