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;
}
Related
Does anyone have the experience in dealing with image resolutions? Is it possible doing with Iphone/ipad?
-(UIImage*)processImageRect:(UIImage*)image:(CGSize)size:(CGSize)originalSize {
// Draw image1
UIGraphicsBeginImageContext(originalSize);
[image drawInRect:CGRectMake(0.0, 0.0, size.width, size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
return resultingImage;
}
//This function will be helpful to you.
I am working on creating an image gallery which has thumbnails in different sizes. I want to convert these rectangle thumbnails to square size so that all of them could appear similar in size. I dont mind cropping it from extended portion but I am not sure how to do it. can anyone please help me?
Thanks
Pankaj
You need to use the image in rect method passing in the image and the required bounds...
CGImageRef imageRef = CGImageCreateWithImageInRect([anImage CGImage], requiredBounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
I have added this to a UIImage category (UIImage+Resize) in the following post, you can download the source code as well - Categories example
Well if you use an UIImageView to display your images (wich I am more than sure that you do) you can set it's contentMode property to UIViewContentModeScaleAspectFill. This should 'crop' your image to the boundaries of your UIImageView. In case your image will go out of the boundaries of the UIImageView make sure clipsToBounds is also set to YES.
Let me know if that helps.
I'm using the next method. The input are the UIImage to scale and the size of the UIImageView's frame where the UIImage is. It works when the frame's height and width are equal.
One important thing: I keep the image's ratio. I don't expand the image to cover the full square. If you want to do it you have to change the 'drawInRect' line for [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; and remove the if-else.
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
CGFloat scaleRatio;
if (image.size.width > image.size.height) {
scaleRatio = image.size.height/image.size.width;
}else{
scaleRatio = image.size.width/image.size.height;
}
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, scaleTransform);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
if (image.size.width > image.size.height) {
[image drawInRect:CGRectMake(0, (newSize.height/2)-(newSize.height*scaleRatio/2), newSize.width, newSize.height*scaleRatio)];
}else{
[image drawInRect:CGRectMake((newSize.width/2)-(newSize.width*scaleRatio/2), 0, newSize.width*scaleRatio, newSize.height)];
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
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.
I have an image formed by a number of sublayers on a UIView, and then it takes a screenshot as so:
UIGraphicsBeginImageContext(object.bounds.size);
[object.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then it puts it onto a button:
[objectButton setImage:screenShot forState:UIControlStateNormal];
But the image displays at it's own size, so if the button isn't the same size as the image then i have a problem of it not making the image bigger or smaller.
How can i get it to change the size of the image to the size of the button?
Try this category -
.h file -
#interface UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size;
#end
.m file -
#implementation UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
#end
you can call it like this -
[imageObject scaleToSize:CGSizeMake(weight, height)];
Hope it help!
Try Like this,
[objectButton setBackgroundImage:screenShot forState:UIControlStateNormal];
You can change the contentMode on the UIButton to scale to fit. This will adjust the size of the image to not be larger than the bounds of the button. Also set the image as the background Image of the button instead of the image.
objectButton.contentMode = UIViewContentModeScaleAspectFit;
[objectButton setBackgroundImage:image forStage:UIControlStateNormal];
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];