_NSCFConstantString CGImage]: unrecognized selector sent to instance - iphone

App is crashing with error _NSCFConstantString CGImage]: unrecognized selector sent to instance
when trying app with this code to save image to photo album
int currentimage = _imageScrollView.contentOffset.y / [pictures count];
UIImage *imageToShow = [pictures objectAtIndex:currentimage];
UIImageWriteToSavedPhotosAlbum(imageToShow, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);

By the information that you gave in the comments, you are using an UIImage reference ("imageToShow") that is pointing to a NSString (element from the array), and that is why when imageToShow receive the CGImage selector, crashes.
To solve this problem, you have to put UIImage references to UIImage objects in the array.
UIImage *image0 = [UIImage imageWithContentsOfFile:"image0FullPath.png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:"image1FullPath.png"];
pictures = [[NSArray alloc] initWithObjects:image0, image1, nil];
If the images are in the mainbundle, you can use imageNamed instead of imageWithContentsOfFile. imageNamed receive as input only the name of the file, and search for it inside the mainbundle. imageWithContentsOfFile needs the full path.
Good Luck!

Related

UIImage isKindOfClass issue

This is my code in my application,
[imageview setAlpha:1.0f];
[imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[pages objectAtIndex:swipeCount]]]];
[imageview setFrame:CGRectMake(-300, 0, 1368, 1000)];
Edit:
pages = [[NSArray alloc] initWithObjects:#"page1",#"page2",#"page3",#"page4",#"page5",#"page6",#"page7b",#"page8",#"page9",#"page10a",#"page11",#"page12",#"page13b",#"page14",#"page15",#"page16a",#"page17",#"page18",#"page19",#"page20",#"page21",#"page22",#"page23",#"page24",#"page25", nil];
imageview=[[UIImageView alloc]init];
its working properly, problems except when the app enters background and comes back to foreground shows the following error,
*** -[UIImage isKindOfClass:]: message sent to deallocated instance 0x1e8b10
What wrong with the code?
Please help me out
Yes, you need to allocate memory to your UIImage. What is basically happening is that your image is being temporarily stored in the memory and deallocated upon closing the app so iOS could allocate that memory to more immediate needs. You can fix that as below. I'm also gonna allocate a string since stringWithFormat returns an autorelease String.
NSString *imageName = [[NSString alloc] initWithFormat::#"%#.png",[pages objectAtIndex:swipeCount]];
UIImage *myImage = [UIImage imageNamed:myImage];
[imageview setImage:image];
Try using properties. Make array,imageView as a property and then use
self.pages and self.imageView
see this good article on properties
Objective-c properties

NSLog statement to see if image exist to save to photo album

Want to check if image0.png, image1.png, image2.png , image3.png, image4.png really exist.
for (int i = 0; i<=61; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
How to write NSLog statement to check if image0.png, image1.png, image2.png , image3.png, image4.png really exist. Every time it is saving only image0.png image.
Thanks for help.
You still can use NSFileManager's fileExistsAtPath method to check whether the file does really exist or not.
[UIImage imageNamed:] loads the image from the application bundle. So, if the file is already is the bundle and has right name as what you're invoking, then it should have been loaded. Try deleting the application from your device/simulator and reload it again. Also, make sure that all of your bitmaps are copied to the bundle by checking the "Copy Bundle Resources" build phase of your target.
Another remark: it is useless to load the UIImage into a UIImageView before saving it to the library, unless you need that for some other processing... So this line of code may be redundant:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
And the last line will become:
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
Hope this helps ;)

-[NSConcreteMutableData _isResizable]: issue with UIImage

I am getting the error below:
-[NSConcreteData _isResizable]: unrecognized selector sent to instance 0x9954d30
on this code:
UIImage *cachedImage = [self cachedImageForUrl:self.imageSource];
if (cachedImage) {
self.image = cachedImage;
}
any idea?
Are you using ARC? If not, this can happen when self.image doesn't correctly retain the cachedImage. When cachedImage gets dealloc'ed and self tries to access it, that memory might now point to random other classes (like NSConcreteData). How did you define the #property for UIImage *image?

Why is this leaking memory? UIImage `cellForRowAtIndexPath:`

Instruments' Leaks tells me that this UIImage is leaking:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#.png", [postsArrayID objectAtIndex:indexPath.row]]]];
// If image contains anything, set cellImage to image. If image is empty, try one more time or use noImage.png, set in IB
if (image != nil){
// If image != nil, set cellImage to that image
cell.cellImage.image = image;
}
image = nil;
[image release];
(class cell (custom table view cell) also releases cellImage in dealloc method).
I haven't got a clue of why it's leaking, but it certainly is.
The images gets loaded multiple times in a cellForRowAtIndexPath:-method. The first three cells' image does not leak (130px high, all the space avaliable).
Leaks gives me no other info than that a UIImage allocated here in the code leaks.
Can you help me figure it out? Thanks :)
The code you have there would be correct if image was a #property. You can release a #property by doing self.property = nil because of the way the setter works. The setter releases the old object and sets the ivar to the value. In order to fix this you would need to put [image release] first. What is happening here is that you set image to nil and then you are essentially doing [nil release]. The old image is just floating around somewhere. So to fix this do the following:
[image release];
image = nil;
You are setting it to nil before calling release. So you are releasing nil instead of image.
Change from:
image = nil;
[image release];
to:
[image release];
image = nil;
Little bit code modification
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath
stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#.png",
[postsArrayID objectAtIndex:indexPath.row]]]];
if (image){
cell.cellImage.image = image;
[image release];

To store images from UIGetScreenImage() in NSMutable Array

I'm getting images from UIGetScreenImage() and storing directly in mutable array like:-
image = [UIImage imageWithScreenContents];
[array addObject:image];
[image release];
I've set this code in timer so I cant use UIImagePNGRepresentation() to store as NSData as it reduces the performance. I want to use this array directly after sometime i.e after capturing 1000 images in 100 seconds. When I use the code below:-
UIImage *im = [[UIImage alloc] init];
im = [array objectAtIndex:i];
UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);**
the application crashes.
And I dont want to use UIImagePNG or JPGRepresentation() in timer as it reduces performance.
My problem is how to use this array so that it is converted into image.
If anybody has idea related to it please share with me.
You don't need to release the image in your first code sample there. [UIImage imageWithScreenContents] returns an autoreleased object.
1. UIImage *im = [[UIImage alloc] init];
2. im = [array objectAtIndex:i];
3. UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);
Line 1 allocates and initializes a new UIImage object, which never gets released since you overwrite the pointer on line 2. You are leaking an UIImage in each iteration, and you don't even need to init/alloc a new object.
UIImageWriteToSavedPhotosAlbum([array objectAtIndex:i], nil, nil, nil);
Should work just fine.
Also note Carl Norum answer about releasing an autoreleased object.