How do I resize a UIImageView in the '.m' - iphone

I have a UIImageView that I want to resize when you press a button. I haven't been able to find any on other forums or sources.
Any help appreciated,
user826671

Let me google it for you http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

I have made a sample using slider control.
You can activate this slider on your button press and use it to increase or decrease the size of the images
https://github.com/manishnath/Code/tree/master/zommin

-(void) onButtonClick
{
CGRect frameRect = temp.frame; //temp ur UIMAGEVIEW
frameRect.size.width= your_width;
frameRect.size.height= height;
temp.frame=frameRect;
}

If its about UIImage, you can do it by this way. For UIImageView you can change its frame size.
- (UIImage *) resizeImage:(UIImage *)image newWidth:(CGFloat)width
newHeight: (CGFloat)height
{
CGRect area = CGRectMake(0, 0, width,height);
CGSize size = area.size;
UIGraphicsBeginImageContext(size);
[image drawInRect:area];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

If you want to resize the image view itself, do like
CGRect newRect = imageView.frame;
newRect.size.width = reqWidth;
newRect.size.height = reqHeight;
imageView.frame = newRect;
The image will also be resized if the imageview has the contentMode property UIViewContentModeScaleAspectFit.

Related

Prevent Stretching image from uiimage in iphone sdk

Code snippet
self.newsImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,300,130)];
//set placeholder image or cell won't update when image is loaded
self.newsImage.image = [UIImage imageNamed:#"newsDetail.png"];
//load the image
self.newsImage.imageURL = [NSURL URLWithString:imageBig];
[imageBack addSubview:self.newsImage];
I have one image on 40*40 size but image view size 300*130. How to avoid stretching image.
I want center of the UIImageview.
Thanks in Advance !!!!
Just center the content:
self.newsImage.contentMode = UIViewContentModeCenter;
ou have to set CGSize as your image width and hight so image will not stretch and it arrange at the middle.
- (UIImage )imageWithImage:(UIImage )image scaledToFillSize:(CGSize)size
{
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;
CGRect imageRect = CGRectMake((size.width - width)/2.0f,
(size.height - height)/2.0f,
width,
height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[image drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

iphone sdk button with image

I'm trying to figure out how to do the following:
I have a button that has an image instead of the default look. User clicks button and chooses a photo (using image picker). I want to set the image of the photo to the photo that the user has clicked.
so, I do the following:
[self.activeBtn setImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
Unfortunately, this scales the image into the little box. What is the best way to ensure that the image is cropped by the borders of the button instead of getting scaled into it?
Edit: want to note that I'm currently on the simulator.
You can use this function to create a cropped version passing the height and width of button as arguments
- (UIImage*)thumbnailOfHeight:(int)height andWidth:(int)width fromImage:(UIImage*)image{
UIImage *thumbnail;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
BOOL isWidthGreaterThanHeight = (image.size.width > image.size.height);
float sideFull = (isWidthGreaterThanHeight)? image.size.height : image.size.width;
CGRect clippedRect = CGRectMake(0, 0, sideFull, sideFull);
UIGraphicsBeginImageContext(CGSizeMake(height, width));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContext, clippedRect);
CGFloat scaleFactor = width/sideFull;
if (isWidthGreaterThanHeight) {
CGContextTranslateCTM(currentContext, -((image.size.width - sideFull)/2)*scaleFactor, 0);
}
else {
CGContextTranslateCTM(currentContext,0, -((image.size.height - sideFull)/2)*scaleFactor);
}
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);
[imageView.layer renderInContext:currentContext];
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imageView release];
return thumbnail;
}
You set like
[self.activeBtn setBackgroundImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];

how to reduce imagesize in iphone

Iam new to iphone currently my problem is
i displayed 6 images in image view It was not showing images in imageview I think the imageviews are in large size
so plz send me code for this imageview.
Thank u in advance.
You could use this for image resizing:
- (UIImage *)rescaleImageToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}

How to resize a UIImageView to fit the underlying image without moving it?

I have a UIImageView whose frame, set before the image is loaded, is always too large for the image, so when I try to round the corners, for example, nothing happens.
How can I resize the frame so it's the same size as the underlying image, while ensuring that the center point of the UIImageView does not change?
If you change the bounds of a UIView the center will not change.
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = newImage.size;
imageView.bounds = bounds;
imageView.image = newImage;
try something along the following lines.. not tested
CGSize imageSize = image.Size;
CGPoint oldCenter = imageView.center;
// now do some calculations to calculate the new frame size
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
imageView.frame = rect;
imageView.center = oldCenter;

How to create an image from a UIView / UIScrollView

I have an image in an UIScrollView, that can be scrolled and zoomed.
When the user presses a button, I want the code to create an image from whatever part of the UIScrollView is inside an area I specify with a CGRect.
I've seen code to crop UIImages, but I can't adapt it to do the same for a view, because it uses CGContextDrawImage.
Any thoughts?
Cheers,
Andre
I've managed to get it.
Here's my solution, based on a few different ones from the web:
- (UIImage *)imageByCropping:(UIScrollView *)imageToCrop toRect:(CGRect)rect
{
CGSize pageSize = rect.size;
UIGraphicsBeginImageContext(pageSize);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(resizedContext, -imageToCrop.contentOffset.x, -imageToCrop.contentOffset.y);
[imageToCrop.layer renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
which you call by using:
CGRect clippedRect = CGRectMake(0, 0, 320, 300);
picture.image = [self imageByCropping:myScrollView toRect:clippedRect];