views:

136

answers:

1

Hi, I want to add my UIImage directly into the file instead of converting into UIImagePNGRepresentation or UIImageJPGRepresentation(as it takes time) like:-

UIImage *im = [UIImage imageWithCGImage:ref];
[array addObject:im];
NSData *data = [array objectAtIndex:i];
[data writeToFile:path atomically:YES];

But it is showing error. So there is any way that i can do it. Thanks in Advance.

+1  A: 

your use of the array only obfuscates that you are basically doing:

NSData *data = im;

Which cannot possibly work because im is a UIImage, and not an NSData nor a subclass.

What you want to do is to create a new NSData and initialize it with the content of the image. Since you got a CGImageRef, I suggest using it directly, without using a UIImage in between.

CGDataProviderRef imageDataProvider = CGImageGetDataProvider(ref);
CFDataRef imageData = CGDataProviderCopyData(imageDataProvider);
NSData *data = (NSData*) imageData;

Note that it is OK to cast the CFDataRef to NSData* because CFData is “toll-free bridged” with its Cocoa Foundation counterpart, NSData.

I hope that helps.

(don't forget to release data when done)

Jean-Denis Muys