Loading a GIF file into CGImage - cocoa

obviously both creation functions for PNG and JPEG doesn't help for GIF files,
How is it possible (and easy?) to load a gif file into a CGImage.

For a Mac, the easiest way is:
NSData *data = [NSData dataWithContentsOfFile:#"mygif.gif"];
CGImageRef myCGImage = [[NSBitmapImageRep imageRepWithData: data] CGImage];

Building on JG's answer, for reference.
A solid way to get the "name" of the file is:
NSString *fileName = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"jpg"];
And then continue with JG's solution:
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([fileName cStringUsingEncoding:NSUTF8StringEncoding]);
self.maskedImage = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

Related

hope to save NSImage without image size change

what I hope to get is
1.load image to a NSImage
2.draw some text on the NSImage
3.save the NSImage to a image file
so I used code below to load image to NSImage
NSData *data = [NSData dataWithContentsOfFile:[asset fullFilename]];
NSImage *img = [[NSImage alloc] initWithData:data] ;
if the original image size is
imageOriginalWidth, imageOriginalHeight
the size(width, height) of img will be smaller than original image size, it means that
imgWidth<imageOriginalWidth
imgHeight<imageOriginalHeight
it also means that if I save the NSImage with text to a image file, the new image is smaller than the original one
Your comment welcome

How to convert jpeg image to PICT image on MAC using Cocoa

How to convert JPEG image to PICT image using cocoa.Some script is given below.
NSData *imgData = [NSData datawithContentsOfFile:#"/var/root/Desktop/1.jpeg"];
NSPICTImageRep *imagerep = [NSPICTImageRep imageRepWithData:imgData];
NSData *data = [imageRep PICTRepresentation];
[data writeTofile:#"/var/root/Desktop/save.pict" atomically:No];
This script is not work. and any other alternate method which convert jpeg image to pict image without Applescript.
.
There's a couple problems with your code.
#1) are you certain of the location of that "1.jpeg" file?
#2) you're not looking at the error result of your "writeToFile". On my machine, I can not write to anything inside the "/var/root" directory.
Once you fix up the source and destination paths, you should change your code to something like this:
NSData *imgData = [NSData datawithContentsOfFile:#"/Users/anuj/Desktop/1.jpeg"];
NSPICTImageRep *imagerep = [NSPICTImageRep imageRepWithData:imgData];
NSData *data = [imageRep PICTRepresentation];
NSLog(#"my image data size is %ld", [data length]);
if([data length] > 0)
{
BOOL success = [data writeTofile:#"/Users/anuj/Desktop/save.pict" atomically:NO];
if(success)
NSLog(#"successfully wrote the file");
else
NSLog(#"did not write the file");
}
else
{
NSLog(#"didn't convert the image to a Pict");
}

UIImage not writing to or appearing on Desktop

For whatever reason, I'm not having the UIImage appear on my desktop. I'm using this code as a means of debugging. However, I'm pretty sure that I am receiving an image since the UIImage in the debugger is not null.
UIImage *imgageProfile = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: sUrlPic]]];
// Use this code to debug images
NSURL *aLocalURL = [NSURL URLWithString:#"file:///Users/snuffles753/Desktop/"];
NSData *imageData = UIImagePNGRepresentation(imgageProfile);
[imageData writeToURL:aLocalURL atomically:YES];
-[NSData writeToURL:…] takes a URL that includes the name of the file you'd like created. It will `not take the URL of a folder, and automatically create a file inside of that. So your current code is attempting to overwrite an existing directory, which then fails.
Instead, specify the filename explicitly:
NSURL *aLocalURL = [NSURL URLWithString:#"file:///Users/snuffles753/Desktop/debug.png"];

Getting bitmap data from JPEG image using Cocoa

I need to extract the raw RGB bitmap data from a JPEG or PNG file, with all the bits in the file, not a window or color converted version.
I'm new to Cocoa, but it looks like I open an image using NSImage like this:
NSString* imageName=[[NSBundle mainBundle] pathForResource:#"/Users/me/Temp/oxberry.jpg" ofType:#"JPG"];
NSImage* tempImage=[[NSImage alloc] initWithContentsOfFile:imageName];
NSBitmapImageRep* imageRep=[[[NSBitmapImageRep alloc] initWithData:[tempImage TIFFRepresentation]] autorelease];
unsigned char* bytes=[imageRep bitmapData];
int bits=[imageRep bitsPerPixel];
Then to get the bitmap data there seems to be lots of options: Bitmapimage, CGImage, etc.
What is the simplest approach and if there was a code snippet, that would be great.
Thanks!
You're on the right track. As you noticed, there are lot of ways to do this.
Once you have an NSImage, you can create a bitmap representation, and access its bytes directly. An easy way to get a NSBitmapImageRep is to do this:
NSBitmapImageRep* imageRep = [[[NSBitmapImageRep alloc] initWithData:[tempImage TIFFRepresentation]] autorelease];
unsigned char* bytes = [imageRep bitmapData];
int bitsPerPixel = [imageRep bitsPerPixel];
// etc
Going through the TIFFRepresentation step is safer than accessing the NSImage's representations directly.

NSImage from byte array

I'm trying to display an image in a NSImageView, with an image contained in a Byte array. How can I do this? From what I understand I need to convert my byte[] to an NSData variable and feed that to an NSImage. Is this correct? How do I do it? I've tried casting and that doesn't work, and there doesn't seem to be any conversion built in...
I have tried the following:
Casting:
NSData bytesAsMacVariable = (NSData) imageAsBytes;
Also tried
NSData bytesAsMacVariable = imageAsBytes as NSData;
Finally, tried to pass a byte[] as if it was a NSData.
NSImage imageToShow = new NSImage(imageAsBytes);
None of these will work, and as far as I can see, neither NSImage or NSData has a member function that accepts byte[] for conversion...
You're casting to the object type, but you should cast to pointer-to-object type.
Try something more like
NSData *imageData = [NSData dataWithBytes:byteArray length:arrayLength];
NSImage *image = [[NSImage alloc] initWithData:imageData];
[imageView setImage:image];
[image release];
The pointers are very important.
You can use an NSMutableData, like this:
new NSImage (new NSMutableData (imageAsBytes));

Resources