Large UIImage not scaling down into Scroll View's frame - uiview

i can't seem to use UIViewContentModeScaleAspectFit property to scale my imageView within my scrollview to fit the scrollview size.
- (void)loadView {
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.moilum.com/images/atomicongallery/setmenu/setmenu1.jpg"]]];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:applicationFrame];
[scrollView addSubview:imageView];
scrollView.contentSize = image.size;
scrollView.minimumZoomScale = 0.3;
scrollView.maximumZoomScale = 3.0;
scrollView.clipsToBounds = NO;
scrollView.delegate = self;
self.view = scrollView;
}
i have tried imageView.contentMode = UIViewContentModeScaleAspectFill as well. scrollView.contentmode = UIViewContentModeScaleAspectFill as well. All dont seem to work :(
any way out!?
Cheers!

The UIScrollView will not adjust its zoomScale if you don't implement the - (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView method on the UIScrollViewDelegate.
Something like this
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView
{
NSArray *subViews = [sView subviews];
if([subViews count] > 0){
return [subViews objectAtIndex:0];
}
return nil;
}
(But apparently not setting a delegate at all works too.)

Have you tried,
-(void)loadView
{
CGRect frame = CGRectMake(0, 0, scrollView.frame.size.width, scrolllView.frame.size.height);
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:#"yourimage.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubView:imageView];
scrollView.contentSize = CGSizeMake( imageView.frame.size.width, imageView.frame.size.height);
}
I write this code from my iPad i think this should work,
Let me know if you have any problem.
Good luck.

Related

scrollView with paging enabled not working properly ? (Objective-C)

i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

How to zoom a UIImageView within a UIScrollView?

I have a UIImageView within a UIScrollView and I can vertically scroll, but not horizontal scroll because of how I set the content size. That's exactly how I want.
However, I can't seem to zoom in and out. I set the minimum and maximum zoom scale, but it's not working.
Can someone tell me why?
Thanks.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.scrollView.scrollEnabled = YES;
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
tempImageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = tempImageView.image.size.width / self.scrollView.frame.size.width;
self.scrollView.zoomScale = 1.0;
self.scrollView.delegate = self;
[self.scrollView addSubview:tempImageView];
}
You have to implement the following delegate method for zooming:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
and make sure that you add self.imageView to your self.scrollView instead.
It should look like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
self.scrollView.delegate = self;
[self.scrollView addSubview:self.imageView];
}
To get zooming to work, you need to provide a delegate method that returns the view you wish to get zoomed:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView
}
You will also need to hold on assign tempImageView to a property so that you can refer to it here. If you don't want to hang on to it in a property, you will need some other way of identifying the view, e.g. scrollView.subviews[0] if it is the only subview.
Try this Try this
[scroll setUserInteractionEnabled:YES];
And your Maximum zoom scale is strange. Try with 2,0 to test.

Hardcoded UIView vs. XIB UIView resizing

I'm stumbled on how to layout a UITextField when there is a hardcoded UIImageView. UIImageView is hardcoded, let me show it:
// Image
CGFloat xCenter = self.view.frame.size.width / 2.00;
CGFloat yCenter = self.view.frame.size.height / 2.00;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.imageFrame = CGRectMake(xCenter + 115, yCenter - 425, 250, 250);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.imageFrame = CGRectMake(xCenter - 125 , yCenter, 250, 250);
}
if ([[BNRImageStore sharedStore] imageForKey:self.item.itemKey]) {
self.imageView = [[UIImageView alloc] initWithFrame:self.imageFrame];
self.imageView.image = [[BNRImageStore sharedStore] imageForKey:self.item.itemKey];
[self.imageView.layer setBorderColor:(__bridge CGColorRef)([UIColor blackColor])];
[self.imageView.layer setBorderWidth:3.0];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.imageView];
NSDictionary *nameMap =#{#"imageView": self.imageView,
#"nameLabel": self.nameLabel,
#"nameField": self.nameField};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[nameLabel]-[nameField]-[imageView(==250)]-|" options:0 metrics:nil views:nameMap];
[self.view addConstraints:#[horizontalConstraints]];
}
else {
UIView *imageCanvasView = [[UIView alloc] initWithFrame:self.imageFrame];
imageCanvasView.backgroundColor = [UIColor colorWithWhite:1.00 alpha:0.4];
imageCanvasView.layer.borderWidth = 1.0f;
imageCanvasView.layer.borderColor = [UIColor colorWithWhite:0.00 alpha:0.9].CGColor;
[self.view addSubview:imageCanvasView];
UILabel *addImageLabel = [[UILabel alloc] initWithFrame:self.imageFrame];
addImageLabel.text = #"(tap to add image)";
addImageLabel.font = [UIFont fontWithName:#"AvenirNext-UltraLight"
size:20];
addImageLabel.textColor = [UIColor WHITE_COLOR_DEBUG_FIX];
addImageLabel.textAlignment = NSTextAlignmentCenter;
addImageLabel.hidden = NO;
[self.view addSubview:addImageLabel];
}
Seems it's what I need:
But not in iPad:
Except UIImageView *imageView property, I made the constants with Auto Layout. Since XIB file doesn't recognize imageView I couldn't set the constraints of it. Instead I can use VFL, but constraints intersect at some point. Thus I couldn't managed it out.
Any ideas? (or is it possible in some way?)
I had a similar problem, I resolved it by creating two holder UIViews either on storyboard or programmatically - one holding the form and one holding the hardcoded UIImageView.
If you're using storyboards : Set the constraints on those two holding views and in the subviews of the one containing the form. And just place your UIImageView programmatically in the center of your image-holding view.

Why am I unable to zoom my UIImageView embedded in a UIScrollView?

I literally have the most basic of code and I'm so frazzled as to why it won't zoom:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.bounds.size;
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 10.0;
[self.view addSubview:self.scrollView];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"community1.jpeg"]];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:self.imageView];
}
In the simulator I try to zoom and nothing at all happens. It's a rather large image if that's relevant.
Did you implement -viewForZoomingInScrollView: in your scroll view's delegate? If not, you need to do that, returning the view you want to actually zoom when you pinch.

The property frame of UIViewController's view doesn't work while using a single subview under UIScrollView

Before using UIScrollView's zoom feature, everything is fine.
The code as bellow:
UIScrollView* scrollView = [[UIScrollView alloc]init];
for(int i=0; i < foodIntroCount; ++i) {
UIImage* image = [UIImage imageNamed:#"7_1.jpg"];
UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
imgView.frame = CGRectMake(0, height*i, width, height);
[scrollView addSubView:imgView];
}
scrollView.contentSize = CGSizeMake(width, height*(foodIntroCount+1)) ;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.delegate = self;
_foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
UIView* wineView = _foodWineViewController.view;
wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
[scrollView addSubview:wineView];
After adding zoom feature, code looks like:
UIScrollView* scrollView = [[UIScrollView alloc]init];
UIView* zoomView = [[UIView alloc]init];
for(int i=0; i < foodIntroCount; ++i) {
UIImage* image = [UIImage imageNamed:#"7_1.jpg"];
UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
imgView.frame = CGRectMake(0, height*i, width, height);
[zoomView addSubView:imgView];
}
scrollView.contentSize = CGSizeMake(width, height*(foodIntroCount+1)) ;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.delegate = self;
scrollView.maximumZoomScale = 2.0;
_foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
UIView* wineView = _foodWineViewController.view;
wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
[zoomView addSubView:wineView];
[scrollView addSubview:zoomView];
The problem is:
wineView displayed on the first page, not on the foodIntroCount+1 page.
Your code should consider the previous y position and height both if you want to add subview one after another vertically. So your code should look like below
imgView.frame = CGRectMake(0, height*i+y, width, height);
The code isn't tested though, try this..

Resources