Skip navigation.
 
mlArchiving NSImage
FROM : Eyal Redler
DATE : Sun Dec 26 02:01:51 2004

Hi All,

My document contains an NSImage which originated from a drag & drop, 
paste etc. I've noticed that when I archive the image via NSCoding I 
get a huge file, even for 32k jpeg, png etc. I'm not sure what happens 
exactly but this is really ridicules.
If I understand correctly then I should somehow fetch the right (that 
is "the source") representation and store that (and not the whole 
image) but I'm not sure how to proceed.

Following is a (clearly misguided) attempt at the problem. The idea 
here is that I prefer vector format over raster so if the image is a 
PDF or EPS then I'd take that, else I assume that all rasters are alike 
(which is a mistake I guess) and use tiff.

So, how should I get the "raw" data and how should I store that.

Thanks,


Eyal Redler
------------------------------------------------------------------------
------------------------
"If Uri Geller bends spoons with divine powers, then he's doing it the 
hard way."
--James Randi


// the image is a member in the class I'm encoding


- (id)initWithCoder:(NSCoder*)coder
{
   ERStuff tempStuff;
   NSImage* tempImage;
   
   [coder decodeValueOfObjCType:@encode(ERStuff) at:&tempStuff];
   tempImage=[[NSImage alloc] initWithData:[coder decodeObject]];
   
   return [self initWithNSImage:tempImage stuff:tempStuff];
}

- (void)encodeWithCoder:(NSCoder *)coder
{
   NSEnumerator* enumerator;
   id currRep;
   NSPDFImageRep* pdfRep=nil;
   NSEPSImageRep* epsRep=nil;
   NSData* theData=nil;
   
   // iterate trough the representations looking for stuff we like
   enumerator=[[image representations] objectEnumerator];
   while (currRep=[enumerator nextObject])
   {
       if ([currRep isMemberOfClass:[NSPDFImageRep class]])
           pdfRep=currRep;
       else if ([currRep isMemberOfClass:[NSEPSImageRep class]])
           epsRep=currRep;
   }
   
   
   if (pdfRep)    // best choice is PDF
       theData=[pdfRep PDFRepresentation];
   else if (epsRep) // second is EPS
       theData=[epsRep EPSRepresentation];
   else // no PDF or EPS - this is a raster image so we use TIFF with 
compression
       theData=[image 
TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:0];
   
    [coder encodeValueOfObjCType:@encode(ERStuff) at:&stuff];
   [coder encodeObject:theData];
}

Related mailsAuthorDate
No related mails found.