uitableviewcell resize image - iphone

i load data in a tableview, each row have a simple picture 500x300px that go resize to size of cell.
Is possible resize the images?
tableview.m
NSString *filePath = [[NSBundle mainBundle] pathForResource:receip.image ofType:#"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
cell.imageView.image = image;

I assume you mean resize them before putting them in the cell to improve performance?
You can do this with core graphics as follows:
//define desired size
CGSize size = CGSizeMake(100.0f, 100.0f);
//create drawing context
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
//draw image at new size
[image drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
//capture resultant image
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Be warned though that is a computationally expensive task. You'll probably want to resize all your images in advance, possibly on a background thread, and store the resized versions in an array. You definitely do not want to stick the code above in your cellForRowAtIndexPath method, or your table scrolling performance will be diabolical.

If those images come from an external source (say the web). It's better to first cache the original file and then (like explained by #Nick Lockwood) resize them (remember retina displays need twice the width / height for hi-res displaying). And after that process is complete you can send a signal that the "download" is complete and carry on showing the UITableView
If you already have the images in your app bundle I suggest you make a thumb version of that and add it to your bundle. For better performance o'course.

Related

large image size problems

I am loading images of size 1800x1300 in UIImage.(getting the images from server..)then i am adding that images into Uiscrollview.while scrolling my images ,the app getting crashed.is it a the image size issue.....?thanks in advance.
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imgData = UIImageJPEGRepresentation([UIImage imageWithCGImage:[image CGImage]],0.5);
UIImage *compressedImage = [UIImage imageWithData:mgData];
You should definitely scale your images down, significantly, especially for the iPhone screen size.
You should also compress your images as much as you can without affecting the quality. Your image, I am assuming is somewhere around 1.5MB - 2MB, so that will use up an extreme amount if memory, and cause your device to crash every time.

What is the best way to display thumbnail list in UITableview

I have an app that need to display image(in a folder) thumbnail list in UITableView.
My way is to create the thumbnail of an image when add the image to the folder.
CGSize itemSize = CGSizeMake(100, 100);
UIGraphicsBeginImageContext(itemSize);
[image drawInRect:CGRectMake(0, 0,100, 100)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData* imageData1 =[NSData dataWithData:UIImagePNGRepresentation (image)];
NSMutableString *sss1=[[NSMutableString alloc] initWithString:folderPath];
[sss1 appendString: thumbnailIdString] ;
[sss1 appendString:fileName] ;
[imageData1 writeToFile:sss1 atomically:NO];
[sss1 release];
Then the app display the resized the thumbnail image in UITableView.
It DOES work. But the performance is not perfect.
It needs to load the large image and rewrites the thumbnails to the folder.
Is there any other better solution? I checked Three20, but I am not sure if it can do this.
Welcome any comment
Thanks
interdev
You can load and resize image in a worker thread, and when the image is ready, show this image in the main thread. In order to complete the above behaviors, you need to find a thread-safe way to resize image. The UIGraphicsBeginImageContext() and UIGraphicsEndImageContext() should only run in the main thread.

Resize image in UITableViewCell to not be total height

Current I am trying to show a simple table in my iPhone application where I use UITableViewCell's with the style UITableViewCellStyleValue1 (image to the left, detail-label right-alligned). The cells all have the default height (50.0f). Before I add an image to the cell, I resize the image to be 40x40, so that it is not the total height of the cell (I think that looks ugly).
I do this with this code:
cell.imageView.image = [UIImage imageNamed:#"icon.png"];
cell.imageView.image = [RootViewController imageWithImage:cell.imageView.image scaledToSize:CGSizeMake(40, 40)];
This is all very nice and works flawlessly. But I want to accomplish this also on the iPhone 4 (with the higher resolution screen). The problem is, that everything is scaled without problems on the iPhone 4 but the images appear very pixelated.
The reason for this is ofcourse that everything on the screen is blown up to scale to the new resolution, also the images, so the images should probably be something like 80x80. But when I resize them to 80x80 (originals are 120x120) they appear way to big, because of the scaling thing.
Is there a way to actually make my images not the complete height of a tablecell, but I want them in the higher resolution on the iPhone 4. Should I create a complete new View for this?
Oops, after the first reply I realised that my own written function was missing:
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContextWithOptions(newSize, NO, [[UIScreen mainScreen] scale]);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
As you can see, after the first reply, I tried to get this to work with the method UIGraphicsBeginImageContextWithOptions but somehow this results in an empty image.
I assume you wrote "imageWithImage:scaledToSize:", right?
I further assume you use "UIGraphicsBeginImageContext(yourSize)" within this call. Replace that with "UIGraphicsBeginImageContextWithOptions(yourSize, NO, 2.0)" in case your platform is iPhone 4.
The "2.0" defines the scale factor for points (you define the size in points not in pixels). On pre-retina-display a point is 1x1 pixel. On retina display a point is 2x2 pixels.
Edit:
Make sure you have a high-res version of "icon.png" in your resources called "icon#2x.png". This is automatically loaded in case it is a retina display.

UIScrollView: Is 806k Image too much too handle? (Crash, out of memory)?

I'm loading a 2400x1845 png image into a scroll view. The program crashes out of memory, is there a better way to handle this? mapScrollView is an UIScrollView in IB, along with a couple of UIButtons.
-(void)loadMapWithName:(NSString *)mapName
{
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/path/%#", bundlePath, [maps objectForKey:mapName]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGSize imgSize = image.size;
mapScrollView.contentSize = imgSize;
[mapScrollView addSubview:imageView];
[imageView release];
[self.view addSubview:mapScrollView];
}
UIImages are limited to 1024x1024 on the iPhoneOS. If you want to get around this, I think the only way is to make use of a CATiledLayer.
I don't know (exactly) how accurate this is, but I often use 64 MB physical memory as a guideline when targeting the iPhone OS (therefore attempting to restrict usage to half that amount). Having said that: The raw allocation size is not the issue.
You may be able to work around the issue by using CGImageRef (the lower level opaque image type) -- essentially wrapped by UIImage.
As another poster has stated, the example exceeds the maximum allowed image size. No device (iPhone/iPod) supports images of that scale, so resizing/dividing may be worth consideration.

How to save a stretched image?

How can I save a stretched image?
I have a small(50 X 50) image, that I have stretched while displaying it on a UIImageView (by choosing scaletofit mode.) Now I need to fetch & save that stretched image.
I have tried by using UIImageView's image property, but it gives me original image not that stretched image.
So, do you have any idea how to solve this problem? If then please help me or guide me or at-least give me a hint.
Hoping for your reply.........
Thanks
You have to create an image by copying the bit map of the view's CGContext (core graphic context). This has been asked on stackoverflow before but I can't find the answer right now.
You need to render your UIImageView into a graphic context you create :
UIGraphicsBeginImageContext(yourUIImageView.frame.size);
yourUIImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//get the PNG representation of the UIImage (but you could do the same with JPEG
NSData *myData = UIImagePNGRepresentation(screenshot);
//then save it to a file
[myData writeToFile:thePathToWriteYourFileAsNSString atomically:YES];