iPhone : Exploring the Foundation Framework – NS Data Class


NSData and its mutable subclass NSMutableData provide data objects, object-oriented wrappers for byte buffers. Data objects let simple 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.

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

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

// 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);

//How to know size of an image:

Int size = [data length];
If (size /1024>0)
  NSLog (@  “ image size is %d  kb”,  size/1024);
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:@”]];
UIImage   *URLImage   =[UIImage ImageWithData : URL data];
NSLog (@ “data length is %d kb ,[URL data length]1024];

//Setting the image as background image for view:

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


Get Updates on Tech posts, Interview & Certification questions and training schedules