iphone: Scale the image in table cell - iphone

I am using a UITableViewCellStyleSubTitle. I am trying to add thumbnails to my table cells.
UIViewContentModeScaleAspectFit does not seem to work for reasons unknown. I have seen a lot of answers in stack overflow for similar kind of questions. If i crop the image using drawInRect it works, like this
[thumbnail drawInRect:thumbnailRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
But what i am trying to achieve should not need me to do this. I am only trying to scale the image and not crop it.
So setting the content mode to "UIViewContentModeScaleAspectFit" for the imageview of the default cell should work.. but it does not.
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
Complete logic for setting thumbnails.. fPath is the path to image
UIImage *thumbnail = [UIImage imageWithContentsOfFile:fPath];
[cell.imageView setImage:thumbnail];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.clipsToBounds = YES;

I found out that drawInRect stretches the original image if the size is lower or croppes it if it is bigger.
This article will help you to prepare a thumbnail of your image
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
It has several useful UIImage classes extentions:
* UIImage+Resize
* UIImage+Alpha
* UIImage+RoundedCorner
Example Usage:
UIImage *imageThumb = [YourImage thumbnailImage:70 transparentBorder:0 cornerRadius:10 interpolationQuality:kCGInterpolationHigh];
and then you can use drawInRect method. If the image is not rectangle - the image will crop the bigger side and take the middle of the image
Hope this answer will guide you.

Related

save an image with two UIImageView which has two images - ios [duplicate]

This question already has answers here:
How can i add two uiimageview and save it to iphone photo gallery in iphone application development?
(2 answers)
Closed 9 years ago.
In my application I have two UIImageViews which is overlayed one over the other. i.e ) UIImageView-2 is placed on UIImageView-1. The image in UIImageView-2 is transparent, so now I have image over another image. Now I need to save these two as a single image. Is it possible in ios? Please advice..
The sample view is as below: The red mark is an image in a view over tennis ball image view.
Hope following method will help you.
-(CGImageRef )mergedImageFromImageOne:(UIImage *)imageOne andImageTwo:(UIImage *)imageTwo
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGSize imageSize = imageOne.size;
UIGraphicsBeginImageContext(sizeVideo);
[imageOne drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
[imageTwo drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height) alpha:1];
CGImageRef imageRefNew = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage, CGRectMake(0,0,imageOne.width,imageOne.height));
UIGraphicsEndImageContext();
[pool release];
return imageRefNew;
}
I've taken one Logo Image which is set on main image as application ref.
So you can refer this, try with your images.
set width(w) & height(h) as per your image.
Try this ::
UIImage *img_Logo = [UIImage imageNamed:#"Img_Logo.png"];
CGSize newSize = CGSizeMake(w, h);
UIGraphicsBeginImageContext(newSize);
// Use existing opacity as is
[main_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[img_Logo drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
main_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgView.image = main_image;
Hope, this'll help you.
Thanks.
Why you are not taking screenshot for only that portion??

UIImageView rotates my UIImage

I'm trying to show an image taken through the camera in Portrait mode but I always get it shown on my UIImageView in Landascape mode. The image is scaled before being added to the UIImageView but it seems this is not the problem as I tried many different solutions found on the web (even some quite smart ones like the one coming from Trevor's Bike Shed).
Here is my code:
UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
CGRect newFrame = [scrollView frame];
UIImage *resizedImage = [ImageViewController imageFromImage:image scaledToSize:newFrame.size];
imageView = [[UIImageView alloc] initWithFrame:newFrame];
[imageView setImage:resizedImage];
[scrollView setContentSize:imageView.frame.size];
[scrollView addSubview:imageView];
[imageView release];
imgPath is the path of the image coming as a parameter and scrollView is an IBOutlet linked to a UIScrollView.
Is there something I'm missing about the UIImageView?
As I wrote above, it seems that the problem is not related to the scaling...
Are you certain that imageOrientation is set as you expect on resizedImage? Incorrect scaling can absolutely mess up imageOrientation.
There is an imageOrientation property for a UIImage. Check out the docs. It can be set through the initializer:
UIImage *rotatedImage = [UIImage imageWithCGImage:[resizedImage CGImage] scale:1 orientation:UIImageOrientationUp]
Creates and returns an image object with the specified scale and
orientation factors.
+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
Parameters:
imageRef The Quartz image object.
scale The scale factor to use when interpreting the image data.
Specifying a scale factor of 1.0 results in an image whose size
matches the pixel-based dimensions of the image. Applying a different
scale factor changes the size of the image as reported by the size
(page 12) property.
orientation The orientation of the image data. You can use this
parameter to specify any rotation factors applied to the image.
Return Value A new image object for the specified Quartz image, or
nil if the method could not initialize the image from the specified
image reference.
UIImage* image=[UIImage imageNamed:#"abc.jpg"];
UIImageOrientation orientation=image.imageOrientation;
Use image in imageView or where ever you want to use,
UIImageView* imageView=[[UIImageView alloc] initWithImage:image];
After that reset the image to orientation it was before usage
UIImage* image1=[UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:orientation];
image1 is your image in the orientation it was before

How to create a new image from an existing image and a string over it

I need to create something like:
http://t3.gstatic.com/images?q=tbn:ANd9GcQZu06WspVUaFTn-C-YD6Pzu_oWH5NLA1Q8nPUZEdemllFIAMNn
So I need to place a user input string over an image in a specified spot, then save it in the memory.
How can I do it?
Create a new bitmap image context. See this for help creating-and-drawing-on-a-new-uiimage
Draw your UIImage in drawInRect
Create black, rounded and transparent image. See this for help:
resize-a-uiimage-the-right-way
Draw black image drawInRect
Draw your NSString text. see this for help how-to-display-text-using-quartz-on-the-iphone drawInRect or drawInPoint
Here is a small code example for cropping an image.
- (UIImage *)croppedImageWithRect:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect(scaleImage.CGImage, bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
Enjoy.
use a UItextView over an image to show such string.

reduce image size in image view in iphone application

I need to reduce image size in iphone.
I found sending programmatically email in google,But did n't found clear info regarding reduce size of image view.
Can any one pls post some sample code.
Thank u in advance.
you can do it like:
UIImage* newImage = nil;
UIGraphicsBeginImageContext(newFrame); // this will crop
[sourceImage drawInRect:newFrame];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This will draw the image in the frame you specified. Now if you want to maintain the aspect ratio also you can have look at Maintain the aspect ratio of an image?
As I saw that you are using image view, so don't use first part of code I mentioned. Now you have to do only:
[imageView setContentMode:UIViewContentModeScaleAspectFit];
Do you mean scaling a view that an image is contained within, or do you mean you want to resample image data to make an image with a smaller file size?
First couple of results in google bring up this... Resize Image
Instead take UIButton and set the image as its backgroundImage property... It will automatically resize the image to the size of the button.
-(UIImage*)createThumbNailImage:(UIImage*)myThumbNail:(float)width:(float)height
{
// begin an image context that will essentially "hold" our new image
UIGraphicsBeginImageContext(CGSizeMake(width,height));
// now redraw our image in a smaller rectangle.
[myThumbNail drawInRect:CGRectMake(0.0, 0.0, width, height)];
// make a "copy" of the image from the current context
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [newImage autorelease];
}
hAPPY iCODING...

UIViewContentModeScaleAspectFit iphone sdk gives poor quality image

Hopefully a quick one? I am creating a custom uitableviewcell and have added an imageview.
I have some PNG images which are around 200x200 in size. I want to create a thumbnail to put in the tableview, but when I resize the image, it results in a poor quality image.
I use UIViewContentModeScaleAspectFit to resize it to a 50x50 frame.
Should I be calling a better draw resize on each image before I Put it to the table cell? There will be around 20-40 images in each table, so I don't want to over work the device!!
Thanks for any help.
Rescaling the images yourself with CoreGraphics will give you more control over the quality, but your best bet is to size the images appropriately for your table in the first place -- less work the software has to do and complete control over the image's appearance.
If you still want to resize them in Quartz, here's one way you might go about it:
UIImage* originalThumbnail = [UIImage imageWithContentsOfFile:<PATH_TO_IMAGE>];
CGSize originalSize = [originalThumbnail size];
CGSize cropSize = { 50, 50 };
CGRect cropRect = CGRectMake(abs(cropSize.width - originalSize.width)/2.0, abs(cropSize.height - originalSize.height)/2.0, cropSize.width, cropSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([originalThumbnail CGImage], cropRect);
// here's your cropped UIImage
UIImage* croppedThumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
You'd want to do this once if possible, i.e. not every time you construct your UITableViewCell.