I have an Image and an ImageView, the ImageView occupies the whole screen and I want to resize the image to fit the ImageView. The ImageView is inside a ScrollView with only vertical scroll bar enabled, which means the Image should be resized with maximum width equal to that of the screen(ImageView), in the mean time keeping the ratio, not constrained on the resized height. The ImageView's height will fit with resized Image, if the height exceeds the height of the screen we can use the scroll bar to see it.
So I want suggestions on:
1) How I can resize an Image with a fixed width if the original width is less than the desired one, no resize is required. No constraint on height.
2) How I can make the ImageView fit the height of the Image automatically?
First, you can calculate the new image's height by yourself.
(if, image's size = (300,200), imageView's width = 320, the new image's height = 200*(320/300))
Then, from this thread The simplest way to resize an UIImage?
You can resize the Image with new width and height.
Total soultion:
UIImage* i= /* original image */;
UIImage* j =[self imageWithImage:i scaledToSize:newSize]; //new Image
[theImageView setImage:j];
(And this code also works fine with iOS 4.x)
Related
I'm trying to implement a simple control to allow the user to zoom in and out of an image. I have a UIImageView inside a UIScrollView. Additionally, I would like to prevent the user from zooming out so much that either the width or height of the image is smaller than the scroll view's size. Here is where the problem lies: when I set the minimumZoomScale to the appropriate size, the image appears in a weird location. Here's my code to configure the scroll view and image view:
- (void)openNewImage:(UIImage *)image
{
_originalImage = image;
// Reset scroll view's zoom scales
// (must be reset before setting the image to the image view)
self.imageScrollView.zoomScale = 1.0f;
self.imageScrollView.minimumZoomScale = 0.01f;
// Set scroll view's content size to allow scrolling
self.imageScrollView.contentOffset = CGPointZero;
self.imageScrollView.contentSize = image.size;
// Set image and resize image view to image size
self.imageView.image = _originalImage;
self.imageView.frame =
CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[self.imageScrollView zoomToRect:self.imageView.frame animated:YES];
self.imageScrollView.minimumZoomScale = self.imageScrollView.zoomScale;
}
The last line is causing problems because if I comment it out, things appear where they should, but then the user can zoom out too much. While trying to debug the problem, I found that the last line causes the image view's frame origin to change to very small negative numbers, like -1.11015e-06 rather than just 0. If I comment out the last line, the image view's frame origin is 0. I wonder if somehow this is causing problems, although that small negative number is virtually 0.
I have UITableview which has custom cells with UIImageViews. In the UITableview I am increasing the height of the cell using didSelectRowAtIndexPath method. Also i am adding method to increase cell height and revert to normal height.
When i tap on the cell the height is increasing , when i tap on the cell again (in order to make the tableviewcell return to default height) while in transition the image which is on tableviewcell is stays like its shown below and goes back to default height ..(i dont know how the image is being displayed while in transition)
-(void) zoomOutProperties
{
fruitImage.frame=CGRectMake(0.0, 46.0, 320.0, 83.5);
backgroundCellImage.frame=CGRectMake(0.0, 0.0, 320.0,129.5);
customCellView.frame=CGRectMake(0.0, 0.0, 320.0, 129.5); // UIView
}
Please note ::
[UIImage imageNamed:#""] caches the image. Instead use: [UIImage imageWithContentsOfFile:#""]
To make the image equal to the UIImageView size, you should use:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
Refer: https://stackoverflow.com/a/9945160/667586
Your numbers for the frames don't add up very well. For instance, in zoomInProperties, you set fruitImage to have origin.y at 73.0, but its height is 82.5 (fractional pixels?) so it will end at 155.5, which is below the bottom of the customCellView which is 135.6. If the outer cell UIView is not set to clip, then the image will draw partly into the next cell.
Also, by setting the inner frames before setting the outer customCellView, you could have 2 changes to the sizes if they have autoResizeMask applied.
I implement the grid view cell size with this method:
- (CGSize)cellSize {
return _gridMode ? CGSizeMake(100, 100) : CGSizeMake(320, 345);
}
And check
_gridMode
for determining which cell to display (either a thumbnail or a full-screen cell).
The full screen cell is supposed to be 320x345.
It draws no subviews wider than 320px.
When I draw the full screen cell, I cannot scroll and when I switch back to grid mode, I see nothing (black screen).
When I set cellSize to 310x345 instead of 320x345, it works -- except my full screen subvies get offset by a few pixels horizontally.
What should I do to display full-screen width AQGridViewCells?
I have a uiview subclass that i want to expand evenly. I mean I want the origin point of the view to be in the center, and have the width and height expand the same amount. Right now the origin point is in the top left, (0,0), and it expands down to the right. How would I go about doing this?
When you change the width and height of the frame, also change the x and y position of the frame by half those amounts.
For example, if your frame is currently (40, 60, 100, 200) and you want it to be wider by 20 and higher by 30, make it (30, 45, 120, 230).
I suggest, if I understand you problem that you get the size of the superview in which the view is located. Let this view be view. Then, let your view be myView.
So all you have to do is the following :
Take half of the size of the screen for width and height values, and to position the origin of your view.
UIView view = myView.superview;
if (nil != view) {
// Make sure your view won't extend by itself with this
myView.autoresizingMask = UIViewAutoresizingNone;
// Get The size of the parent view
CGSize size = view.bounds.size;
// Set the new frame of your view
myView.frame = CGRectMake(size.width/2.0f,
size.height/2.0f,
size.width/2.0f,
size.width/2.0f);
}
And now your view is positioned correctly
what is the best way for horizontal scrolling of image in iphone sdk. Image has to be get from web server something like lazy loading?
please help me.
Here is my suggestion:
First, set fixed size for your UIScrollView.
scroll.size = ... // your size
Next, determine width and height for contentSize of your scroll. Fix the height. the width is the value changeable. After each lazy image is loaded successfully, adjust the width.
Assume, all the image have the same height. Then, each time when there's an image is loaded, the code is:
(in the below code, I assume that the new image is appended. In your case, you can configure properly for your need)
float oldScrollWith = scroll.contentSize.width
// creat new_image_view (UIImageView)
// configure it to display in the scroll
...
new_image_view.frame = CGRectMake(oldScrollWidth, 0, imageViewWidth, fixed_image_height)
// append the new view to the scroll
float newScrollWidth = oldScrollWidth + new_image_view.frame.size.width // re-calculate the width for the contentSize of scroll
scroll.contentSize = CGSizeMake(newScrollWidth, fixed_image_height);
[scroll addSubview:new_image_view];