I have a UITableView which uses the following code to display an image in a Table View cell:
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
UIImage *image = [UIImage imageNamed:[[color types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
if ( image ) {
cell.imageView.image = [image imageScaledToSize:CGSizeMake(50, 50)];
}
It works fine on the iPhone simulator, but when I try it on a real iPhone the iPhone doesn't show. Instead in the console in debugging mode, I get this error:
attempt to pop an unknown autorelease
pool (0x851e00)
Any help would be great, thanks.
Check that image != nil. If it == nil then problem in that [[color types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] code.
Generated file name must be equal real file name including extension, case and must not contain whitespaces.
Related
I have been testing an IOS app on multiple platforms and have observed some weird behaviours while testing on the iPhone 4S. The app crashes after performing a task that works on both the iPhone 5 and iPhone 3S. When the crash happens there is nothing displayed on the debugger, absolutely no crash log or memory warnings appear in Xcode. I isolated the block of code, and the specific line responsible for the crash. I still don't understand why it is happening or how to find a solution. The code is part of a larger method that downloads a number of images and displays them in UIImageViews. Here is the code:
//fill image tiles with images
for (int i = 0; i < [objects count]; ++i)
{
PFObject *imageObject = [objects objectAtIndex:i];
PFFile *imageFile = (PFFile *)[imageObject objectForKey:#"image"];
UIImageView *imageHolder = (UIImageView *)[self.view viewWithTag:(100 + i)];
imageHolder.image = [UIImage imageWithData:imageFile.getData];
}
The code loops through an object that has been loaded with image files from a server. The UIImageViews that each image is meant to be assigned to are tagged from 100 - 104, so they can be accessed with the loop index variable. The line UIImageView *imageHolder = (UIImageView *)[self.view viewWithTag:(100 + i)] extracts the UIImageViews from the main view. In the next line the imageHolder view is assigned an image, this is the line that causes the crash, and when commented out the view loads without fail. I haven't been able to determine why this is happening or if it is the imageFile or imageHolder view that is not being set up properly. Perhaps someone here can shed some light on the problem.
You should have some protection around imageFile.getData as it could return nil. You should preferable use the getData: method which returns an error that you can use if the data can't be accessed.
NSError *error = nil;
NSData *imageData = [imageFile getData:&error];
if (imageData != nil) {
imageHolder.image = [UIImage imageWithData:imageData];
} else {
NSLog(#"OMG!! %#", error);
}
In IOS (iPhone) - I want to show the user an Image, he should write a text string (like the image name or something), If he writes the right string the image changes to a different image, if he makes a mistake and type the wrong string, nothing happens
take user's entered string into
NSString *imageName = userTxtField.text;
UIImage *image = [UIImage imageNamed:imageName];
if(image != nil){
// change previous image
}
May be you are trying to build an app like "Brands"
Once user types and press submit button compare imagename with user string
if([textField.text isEqualToString:IMGNAME]) {
imageview.image = nextimage;
}
You should use ImageNamed function of UIImage class.
If return value is nil then an image doesn't exist.
For example:
-(UIImage*)getImageNamed:(NSString*)name{
UIImage* Image = [UIImage imageNamed:name];
if (!Image)
NSLog(#"Sorry, there is no image named %#.",name");
return (Image);
}
Hi I have this code to download images from the server and then to show them in table view with some text (if there is no image I want to show the text anyway):
//download part
NSMutableString *photourl=[[NSMutableString alloc] initWithString:url_image];
[photourl appendString:[dic objectForKey:#"photo"]];
#try{
//add to mutable array
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:photourl]]];
}
#catch (id Exception) {
[photoSmallImageData addObject:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_image]]];
}
//part of table view code
NSData *imageData = [photoSmallImageData objectAtIndex:indexPath.row];
#try {
cell.imageView.image = [UIImage imageWithData:imageData];
}
#catch (NSException *exception) {
cell.imageView.image = [UIImage imageNamed:#"icon72.png"];
}
It works but the thing is sometimes there is no image so I would like to replace it with some icon but since we can't add nil to array i juste add some part of my link there and then even if the link doesn't work for image data it doesn't call the catch method. I don't know if I could some how replace the url with local url but I don't know how. Also I can't skip the step because then the images will not correspond to the text next to it.
2.The images are all of diferent sizes and I would like them to have standard look and be centered is there a way to do that and at which point of the code? Thank you very much
EDIT I have found the method:
dataWithContentsOfURL:options:error:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
but I don't know how to use it
Why don't you create the images when the image data is downloaded and keep them in the array? Also, you are right to be after the dataWithContentsOfURL:options:error: method. Given that you keep the image names in the dic dictionary, the code would look like this:
NSMutableString *photourl=[[NSMutableString alloc] initWithString:url_image];
[photourl appendString:[dic objectForKey:#"photo"]];
//try to create the image
NSError* err = nil;
UIImage* img = nil;
NSData* tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:photourl] options:NSDataReadingMappedIfSafe error:&err];
if (!err) {
// image data loaded, create image
img = [UIImage imageWithData:tempData];
} else {
// image data could not be loaded, display error
NSLog(#"%#", err);
// put the default image in the array
img = [UIImage imageNamed:#"icon72.png"];
}
[photoSmallImage addObject:img];
And then in the table view delegate:
cell.imageView.image = [photoSmallImage objectAtIndex:indexPath.row];
As for the second part of your question, you may want to have a look at the contentMode property of UIView class. The default is scale to fill. By manipulating this property on the cell.imageView, you can achieve the effect that you desire.
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];
Im developing an app for an iPhone and I found that the following code is causing the memory allocation to increment.
-(UIImage *)createRecipeCardImage:(Process *)objectTBD atIndex:(int)indx
{
[objectTBD retain];
// bringing the image for the background
UIImage *rCard = [UIImage imageNamed:#"card_bg.png"];
CGRect frame = CGRectMake(00.0f, 80.0f, 330.0f, 330.0f);
// creating he UIImage view to contain the recipe's data
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = rCard;
[rCard release];
imageView.userInteractionEnabled = YES;
float titleLabelWidth = 150.0;
float leftGutter = 5.0;
float titleYPos = 25.0;
float space = 3.0;
float leftYPos = 0;
// locating Title label
float currentHeight = [self calculateHeightOfTextFromWidth:objectTBD.Title :titleFont :titleLabelWidth :UILineBreakModeWordWrap];
UILabel *cardTitle = [[UILabel alloc]initWithFrame:CGRectMake(leftGutter, titleYPos, titleLabelWidth, currentHeight)];
cardTitle.lineBreakMode = UILineBreakModeWordWrap;
cardTitle.numberOfLines = 0;
cardTitle.font = titleFont;
cardTitle.text = objectTBD.Title;
cardTitle.backgroundColor = [UIColor clearColor];
[imageView addSubview:cardTitle];
[cardTitle release];
leftYPos = titleYPos + currentHeight + space;
// locating brown line
UIView *brownLine = [[UIView alloc] initWithFrame:CGRectMake(5.0, leftYPos, 150.0, 2.0)];
brownLine.backgroundColor = [UIColor colorWithRed:0.647 green:0.341 blue:0.122 alpha:1.0];
[imageView addSubview:brownLine];
[brownLine release];
leftYPos = leftYPos + 2 + space + space + space;
// creating the imageView to place the image
UIImageView *processPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(leftGutter, leftYPos, 150, 150)];
if((uniqueIndex == indx) && (uniqueImg.imageData != nil))
{
if([uniqueImg.rcpIden isEqualToString:objectTBD.iden])
{
objectTBD.imageData = [NSString stringWithFormat:#"%#", uniqueImg.imageData];
[recipesFound replaceObjectAtIndex:indx withObject:objectTBD];
NSData * imageData = [NSData dataFromBase64String:objectTBD.imageData];
UIImage *rcpImage = [[UIImage alloc] initWithData:imageData];
[imageData release];
processPhoto.image = rcpImage;
[rcpImage release];
}
}
else if(objectTBD.imageData != nil)
{
NSData * imageData = [NSData dataFromBase64String:objectTBD.imageData];
UIImage *rcpImage = [[UIImage alloc] initWithData:imageData];
processPhoto.image = rcpImage;
[rcpImage release];
[decodedBigImageDataPointers addObject:imageData];
}
else
{
UIImage * rcpImage = [UIImage imageNamed:#"default_recipe_img.png"];
processPhoto.image = rcpImage;
[rcpImage release];
}
NSlog(#" Process Photo Retain Count %i", [processPhoto retainCount]); // this prints a 1
[imageView addSubview:processPhoto];
NSlog(#" Process Photo Retain Count %i", [processPhoto retainCount]); // this prints a 2!!!!
//[processPhoto release]; // this line causes an error :(
// converting the UIImageView into a UIImage
UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[objectTBD release];
for(UIView *eachSubview in imageView.subviews)
{
[eachSubview removeFromSuperview];
NSLog(#"each subview retainCount %i despues", [eachSubview retainCount]);
// here I find that the processPhoto view has a retain count of 2 (all other views have their retain count in 1)
}
return viewImage;
}
When I checked at the instruments object allocation I found that the "GeneralBlock-9216" growing up.
Breaking down the row I found that every time I call this code, one instance of:
2 0x5083800 00:18.534 ImageIO initImageJPEG
is being allocated. Checking the call stack, the following line is highlighted:
UIImage * objImage = [UIImage imageWithData:imageData];
Any help to find what the error is?
As TechZen said, the imageWithXXX: methods cache the image inside of them while you run the program (though you release the instances after using). I recommend initWithXXX: and release API sets instead of imageWithXXX:.
Well, if you embed several debug log on your source code, check how many times is it called, and check the retain count of the instances.
As far as I can explain, that is all.
I hope you will solve the problem.
Does anyone have an answer for this? It's tearing me apart trying to figure out why this image information keeps lingering. I've tried every solution.
The situation:
Images get downloaded and stored to the device, then loaded with imageWithContentsOfFile (or even initWithContentsOfFile, which doesn't help either). When the view goes away, the images don't, but they don't show up as leaks, they're just this initImageJPEG Malloc 9.00 KB that never goes away and keeps ramping up.
UPDATE: I believe I've figured this out: Check to make sure everything is actually getting dealloc'd when you're releasing whatever the parents (and/or grandparents) and etc of the images are. If the parents don't get deallocated, they never let go of their children images, and whatever data was in those images sticks around. So check retain counts of parent objects and make sure that everything's going away all the way up whenever you release the view at the top.
A good way to check for this is to put NSLogs into custom classes' dealloc methods. If they never show up, that object isn't going away, even though the reference to it might, and it (and whatever its subviews and properties are) will never ever disappear. In the case of images, this means a pretty sizable allocation every time that object is generated and never deallocated. It might not show up in leaks, especially if the parent of the topmost object you're thinking you're releasing but actually aren't persists and doesn't itself ever deallocate.
I hope this helps. It'll be useful to take some time to read through your code with a fine-toothed comb to make sure you're allocating and releasing things properly. (Search for "alloc]", start at the top of the file, and work your way down to make sure you're releasing and that the release isn't inside of some if() or something.)
Also, running "Build and Analyze" might lock up your machine for a bit, but its results can be really helpful.
Good luck!
I think you're seeing UIImage cacheing images. There used there used to be a method something like initWithData:cache that let you turn the cacheing off. Now I think the system always caches the images automatically behind the scenes even after you've deallocted the specific instances.
I don't think its an error on your part. I think it's the system keeping data around in the OpenGl subsystem. Unless it causes a major leak, I don't think it is a problem.