UIGraphicsGetImageFromCurrentImageContext , alloc version? - ios

UIGraphicsGetImageFromCurrentImageContext returns autoreleased UIImage.
Wonder if there is a way to get "alloc"-ed image from context?
I want something equivalent to following code, maybe the code is best I can do?
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
UIImage* image = get image by UIGraphicsGetImageFromCurrentImageContenxt
[image retain];
[pool release];

Just call -retain on the result.
UIImage *image = [UIGraphicsGetImageFromCurrentImageContext() retain];

Related

Why doesn't UIImage get successfully decoded?

Why doesn't the UIImage in this code snippet get restored back to its original state when I try to encode and decode it using NSKeyedArchiver?
I expect "decodedImage" to contain the image after decoding, but instead it is just NULL.
// Any image here seems to repro the issue
UIImage *image = [UIImage imageNamed:#"soda.jpg"];
// This prints YES (1), just a sanity check.
NSLog(#"Confirms %d", [[UIImage class] conformsToProtocol:#protocol(NSCoding)]);
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[coder encodeObject:image forKey:#"image"];
[coder finishEncoding];
// I would expect this to be large, instead it's < 1kb.
NSLog(#"Data length is: %zu", (unsigned long)data.length);
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
// This prints YES (1)
NSLog(#"containsValueForKey returns %d", [decoder containsValueForKey:#"image"]);
// decodedImage is NULL here, even though containsValueForKey returned YES
UIImage *decodedImage = [decoder decodeObjectForKey:#"image"];
[decoder finishDecoding];
In this case, I'm not looking for a workaround like converting the UIImage to NSData first and encoding that. The reason is that I'm trying to reproduce an unrelated piece of code which uses something like this and I'm trying to understand it.
The code works as expected if I roundtrip the image first through nsdata and back to uiimage, why??
UIImage *originalImage = [UIImage imageNamed:#"soda.jpg"];
NSData *imageData = UIImagePNGRepresentation(originalImage);
UIImage *image = [UIImage imageWithData:imageData];
Check this
Decode Data to image:
+(NSData *)decodeBase64ToImage:(NSString *)strEncodeData
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return data;
}
self.btnLicenseFront.image=[UIImage imageWithData:[Themes decodeBase64ToImage:licenseFront]];
I used your code and tried with 2 images:
1. A correct image file
The output is
> [36133:5889153] Confirms 1
> [36133:5889153] Data length is: 68267
> [36133:5889153] containsValueForKey returns 1
> [36133:5889153] decodedImage is 1879681920
2. Incorrect/corrupt image file
The output is
> [36130:5888794] Confirms 1
> [36130:5888794] Data length is: 136
> [36130:5888794] containsValueForKey returns 1
> [36130:5888794] decodedImage is 0
So looks like your source JPG file is corrupt or invalid.
I have found a solution.
It turns out that this only happens the image is loaded through the [UIImage imageNamed:]. If the UIImage is created through[UIImage imageWithContentsOfFile:], the issue does not happen.
I believe this must be a bug on the ios side. The imageNamed: way of creating a UIImage is specifically for images inside the bundle. There must be some optimization they have which causes NSCoder to not function as intended since the UIImage seems to not actually contain the image data (since decoding seems to return nil instead of recreating the UIImage with the image from the bundle as expected).

UIimage to NSData returns Null

I created a QR code image and displayed it in a UIImageView
NSString *QRMessage = #"MESSAGE";
NSData *data = [QRMessage dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter *filter = [CIFilter filterWithName:#"CIQRCodeGenerator"];
[filter setValue:data forKey:#"inputMessage"];
[filter setValue:#"Q" forKey:#"inputCorrectionLevel"];
imageQRCode = filter.outputImage; // imageQRCode is CIImage
ivQR.image = [UIImage imageWithCIImage: imageQRCode]; // ivQR is UIImageView
I'm trying to save the image so the user can somehow send the QR code to another person. I first tried saving it to the "Clipboard" like this...
UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:NO];
[pasteBoard setPersistent:true];
[pasteBoard setImage:ivQR.image];
... but it appears nothing is saved in the Clipboard.
So then I tried converting the UIImage to NSData and adding it as an attachment like so:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
UIImage *imageToSend = [UIImage imageWithCIImage:imageQRCode];
NSData *data = UIImageJPEGRepresentation(imageToSend,1);
[picker addAttachmentData:data mimeType:#"image/jpeg" fileName:#"QR.jpg"];
But again nothing seems to be attached.
I did some testing and it appears that the NSData I'm getting back from "UIImageJPEGRepresentation()" gives me "Null" data. The image does in fact get displayed on my phone, so I'm wondering if I'm just converting the data wrong?
Most of my "googling" tells me that the way I'm converting is correct. But most of the examples use a Picture on the user's phone, or a Picture added to the App itself. My Picture is "created"... so does that make a difference?
My goal is to allow the user to Copy to Clipboard, or Add as Attachment of the QR Image. Any assistance is greatly appreciated.
When converting a CIImage to UIImage, I've found that imageWithCIImage often doesn't work. I generally use CIContext method createCGImage and then create the UIImage from that:
CIImage *ciImage = ...
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:rect];
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
See samples in Core Image Programming Guide.

cannot create a CIImage

I cannot understand what I'm doing wrong here, but for some reason, no matter how I try, I cannot create a CIImage
UIImage *origImage = [[UIImage alloc] init];
origImage = [UIImage imageNamed:imageName];
imageName = [imageName substringToIndex:[imageName length]-4];
NSURL *path1 = [[NSBundle mainBundle] URLForResource:imageName withExtension:#"jpg"];
NSLog(#"the path is %#", path1);
NSLog(#"the inputImage is %#", imageName);
CIImage *inputImage = [[CIImage alloc] initWithCGImage:origImage.CGImage];
CIImage *inputImage1 = [[CIImage alloc] initWithImage:_originalImage];
CIImage *inputA = [[CIImage alloc] initWithContentsOfURL:path1];
CIImage *empty = [[CIImage alloc] initWithImage:origImage];
A breakpoint at the end of this code shows a UIImage, a string and a url, all of which are as expected.
The header includes CoreImage.h, as well as UIKit. _originalImage is a UIImage property, which is assigned earlier.
Stumped on this for a few days, any help really appreciated. Thanks.
Using your code I get all 4 CIImages, is it possible that there is something with your jpg file?
I resolved this via the project settings.
I have found that if I had set the optimisation level of the Apple LLVM 6.0 Code Generation to something other than None [-00], then none of these images formed.
however, without the optimisation, it works as expected.
Not sure why, but not complaining.

UIImage with NSData initWithData is nil

The following code does not seem to load an image.
uiTabBarItem = [[UITabBarItem alloc] init];
NSData *datatmp = [NSData dataWithContentsOfFile:#"newsicon.png"];
UIImage *tmp = [[UIImage alloc] initWithData:datatmp];
uiTabBarItem.image = tmp;
datatmp is nil (0x000000) and
the image does exist.
I. Don't reinwent the wheel. Use tmp = [UIImage imageNamed:#"newsicon.png"]; instead.
II. NSData expects a full file path when being initialized from a file. The following would work (but you don't have to use this anyway, as I just pointed it out):
NSString *iconPath = [[NSBundle mainBundle] pathForResource:#"newsicon" ofType:#"png"];
NSData *datatmp = [NSData dataWithContentsOfFile:iconPath];
Loading an image from a file is best accomplished with:
[UIImage imageNamed: "newsicon.png"];

problem with accessing NSMutableArray

I have the following problem:
int index=6;
imageView.image=[imageArray objectAtIndex:index];
NSLog(#"%#",[imageArray objectAtIndex:index]);
If I run this code I get (null) as an output...even though I have nicely put the images inside the array using the following code:
NSURL *url = [NSURL URLWithString:#"somelink"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
[imageArray addObject:image];
I am pretty sure there are 20 images (I use an XML file and print the URL and image) and the image is fine, too. I print the values of image before putting inside the array and here's the value I get
is :
<UIImage: 0x5368670>
Can anyone kindly help me out ? Thanks.
Remember you'll need to make a new instance of the NSMutableArray... it's possible you're just calling methods on nil.
Before you start to you the imageArray, make sure you do something like:
imageArray = [NSMutableArray array];
// or = [[NSMutableArray alloc] init] if you want to "retain" it
// for use in other methods
Have you added at least seven such objects to the array, though? Remember that NSArray (and friends) count from zero, not one.
Also, are you certain that the object you're adding is not null (i.e, that dataWithContentsOfURL: and imageWithData: are both succeeding)?

Resources