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...
Related
I've been having issues rendering images with the UIImageView class. The pixelation seems to occur mostly on the edges of the image I am trying to show.
I have tried changing the property 'Render with edge antialiasing' to no avail.
The image files contain images that are larger than what will appear on the screen.
It seems to be royally messing with the quality of the image and then displaying it. I tried to post images here, but StackOverflow is denying me that privilege. So here's a link to what's going on.
http://i.imgur.com/QpUOTOF.png
The sun in this image is the problem I'm speaking of. Any ideas?
On-the-fly image resizing is quick and of low quality. For bundled images, it is worth the extra bundle space to include downsized versions. For downloaded images, you can achieve better results by resizing with Core Graphics into a new UIImage before you set the image property.
CGSize newSize = CGSizeMake(newWidth, newHeight);
UIGraphicsBeginImageContextWithOptions(newSize, // context size
NO, // opaque?
0); // image scale. 0 means "device screen scale"
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[bigImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Use following method use for get specific hight and width with image
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
CGSize newSize = CGSizeMake(width, height);
float widthRatio = newSize.width/image.size.width;
float heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This method return NewImage, with specific size that you specified.
How big is your image and what is the size of the imageView? Don't rely on UIImageView to scale it down for you. You probably need to resize it manually. This would also be a bit more memory efficient.
I use categories like these:
>>>github link <<<
to do image resizing.
This also gives you some other nice function for rounded corners etc.
Also keep in mind, that you need a transparent border at the edge of an image if you want to rotate it to avoid aliasing.
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.
My question is that I want to add crop image functionality in my pocketWallet app. I want to take image from camera and save it in ImageView after that i want to crop that image. I have read many questions like same but it Unfotunatly not found required answer.
Please tell me how can I change imageview size with exact image size that i take from camera.
I am using a uiview to crop the specific part of the image . But when I cropped the image then not get exact part which I want to crop. My cropped image code is below,
-(UIImage *)cropImage{
float tempx=CGRectGetMinX(cropedAngle.frame);
float tempy=CGRectGetMinY(cropedAngle.frame);
CGRect rect=CGRectMake(tempx, tempy, 320, 200);
CGImageRef image = [imgView.image CGImage];
CGImageRef imageRef = CGImageCreateWithImageInRect(image,rect);
UIImage *imge = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Create and show the new image from bitmap
return imge;
}
First of tell my logic is right or not to crop image? In which this code cropedAngle is my uiview which i used to crop the specific part of image .
Where am I wrong?
I want to create a Photos Library as existing photo library in iPhone. I add image in scrollviewer which is chosen from Photo library. Before add image i resize the selected image and set it to ImageView Control.But when i compare to added image quality with iPhone Photo library image quality, my control image is not good. How to bring the quality and withou memory overflow issue.
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I ran into this issue also. I think you're using an iPhone 4 with Retina Display. Even if you're not, you should account for it. Instead of UIGraphicsBeginImageContext(), use UIGraphicsBeginImageContextWithOptions() and use the scale property of UIScreen for the third argument. All iOS devices have the scale property, on iPhone 4 it's set to 2.0; on the rest, as I write this, it's set to 1.0.
So your code, with those changes, becomes
-(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
UIGraphicsBeginImageContextWithOptions(newSize, YES, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Following the solution from ageektrapped, for PNG's transparent images, you should set the second parameter to NO/false for
UIGraphicsBeginImageContextWithOptions(..., NO, ...);
will solve the black/white background issue
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.