On Mon, Oct 12, 2009 at 4:30 PM, Kelvin Chung <[email protected]> wrote:
> I have this "nonstandard" image format which I want to convert into a more > "standard" image format (say, PNG). I'm having difficulty on how to use > NSImageRep to do so. This "nonstandard" format can be converted into a > bitmap, so I thought that using NSBitmapImageRep can do just that. However, > since I have no experience in this area, I need some guidance on how to do > so. So, any general ideas? >From this I assume you have some way to read the data yourself and process it into a form from which you can instantiate an NSBitmapImageRep. Let's call this function basicTranslate. There are few reasonable approaches. (1) A new NSImageRep subclass. Make a subclass of NSImageRep which has an instance of NSBitmapImageRep as an instance variable. In imageRepWithData:, invoke basicTranslate and instantiate the NSBitmapImageRep. Implement the overridden methods of NSImageRep, oftentimes by delegating to the NSBitmapImageRep that you have as an ivar. Some of the methods of NSImageRep describe the types that your rep subclass is capable of reading. Using this info, AppKit will call +imageRepWithData: on your class if it encounters a type that you have signed up for. (2) Use filter services to sign up as a translator for the file format. This is demonstrated in the SimpleImageFilter<http://tuvix.apple.com/mac/library/samplecode/SimpleImageFilter/index.html> example. If you do this, _all_ apps that use NSImage end up able to read your file format. Your image can be dropped directly into TextEdit. (3) Just do it. Make a method +imageWithCrazyFormatData:, and do basicTranslate in there. In any code that wants to read the format, you have to call this method. Nothing in AppKit will automatically call it for you. An advantage of the first approach is that you have a place to put other functionality that has to do specifically with your image file format. For example, maybe your file format has a bunch of special metadata. Your rep subclass is a place to put accessors for that metadata. You also can tell the difference between an image that came from your file format and one that came from a png. The second approach is probably less code than the first, and you transparently add support for your format to all apps that use NSImage. Which is kind of cool. The third approach uses the least framework infrastructure, so it's probably the easiest to understand. And it's totally fine and straightforward if your format only should be read in specific places where you know that's what you're doing. -Ken _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
