/* NSString to NSData */NSString*theString=@"This is a string";NSData*theStringData=[theStringdataUsingEncoding:NSUTF8StringEncoding];NSLog(@"%@\n%@",theString,theStringData);/* NSData to NSString */NSString*theStringFromData=[[NSStringalloc]initWithData:theStringDataencoding:NSUTF8StringEncoding];NSLog(@"%@",theStringFromData);[theStringFromDatarelease];
/* NSImage to NSData (TIFF) */NSData*someData=[NSDatadataWithContentsOfFile:imagePath];NSImage*someImage=[[NSImagealloc]initWithData:someData];NSData*dataFromImage=[someImageTIFFRepresentation];NSMutableString*homeDir=[[NSMutableStringalloc]initWithString:NSHomeDirectory()];[dataFromImagewriteToFile:[homeDirstringByAppendingPathComponent:@"Desktop/image.tiff"]atomically:NO];/* NSImage to NSData (Any Format, use png as example) *///NSBitmapImageRep *bmprep = [NSBitmapImageRep imageRepWithData:dataFromImage]; // TIFF representation dataNSBitmapImageRep*bmprep=[NSBitmapImageRepimageRepWithData:someData];// PNG data from fileNSDictionary*imageProperty=[NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithFloat:0.0],NSImageCompressionFactor,nil];NSData*otherTypeImageData=[bmpreprepresentationUsingType:NSPNGFileTypeproperties:imageProperty];[otherTypeImageDatawriteToFile:[homeDirstringByAppendingPathComponent:@"Desktop/image.png"]atomically:NO];[homeDirrelease];[someImagerelease];
/* NSString to NSURL */NSString*google=@"http://google.com";NSURL*googleURL=[[NSURLalloc]initWithString:google];NSLog(@"%@",googleURL);/* NSURL to NSString */NSLog(@"%@, %@",[googleURLrelativeString],[googleURLabsoluteString]);[googleURLrelease];
/* CGImage to NSImage */NSBitmapImageRep*bitmapRep=[[NSBitmapImageRepalloc]initWithCGImage:cgImage];NSImage*image=[[NSImagealloc]init];[imageaddRepresentation:bitmapRep];[bitmapReprelease];[imageViewsetImage:image];[imagerelease];