Krunal
Krunal

Reputation: 6490

UIImage does not scroll in iPhone

I am new to iPhone developer,

I want scrolling in my Image,

Here is my code snippet,

- (void)viewDidLoad {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

 [self centerScrollViewContents];
}


- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.imageView.frame = contentsFrame;
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}

also bg.png is not getting applied to my background.

Any help will be appreciated.

Upvotes: 5

Views: 1828

Answers (8)

Bajaj
Bajaj

Reputation: 869

scrolling the images

scroll.userinteractionEnable = YES;

and other then,

self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.png"]]];

Upvotes: 0

john.k.doe
john.k.doe

Reputation: 7563

the answer is actually a combination of many of the other answers & comments, plus some other adjustments.

  1. as Pan said, you need to set your scrollView delegate. otherwise, your viewForZoomingInScrollView: delegate implementation won't get called. i've provided this call in viewDidLoad: , though you can also hook this up in interface builder or storyboard.
  2. as Teodor Carstea said in the comments to the original post, in your original code, you are setting your .imageView.size and your .scrollView.contentsSize to exactly the same thing. you don't need the code that sets the .imageView.frame. that will happen as part of initWithImage: . and this will make the sizes different.
  3. you do need to set the .scrollView.contentSize to the image.size . it sort of matters where ou do this, at least in terms of that it must happen prior to setting the zoom scale.
  4. as Kunal Balani mentioned, you have to have scrollingEnabled, though you can set this in storyboard or interface builder.
  5. as MidhunMP said, you have to have .userInteractionEnabled = YES, though generally, these are the defaults that get set in storyboard/Interface Builder, so i'm not going to include them in the code changes below.
  6. your method centerScrollViewContents isn't really centering the contents as much as it is re-shaping the imageView frame. i've simply commented out the call, because i think it's completely unnecessary. instead, i've made a simple call to start your image in the upper left corner.
  7. if you only want to scroll, setting the zoomScale, minimumZoomScale and maximumZoomScale is not necessary. if you also want to pinch-zoom, then you will need to set all three of these, and in the proper places.
  8. you do seem to want to stretch the view if it is smaller than the content bounds. if this is truly a concern (i.e. if you know your bg.png is so small that it needs to be stretched), then calculate the contentFrame to image frame width ratio and height ratio separately, and whichever of these is larger will be your new zoomScale, and you'll be able to scroll in the other direction. or, there are other ways to decide how you want that scale set. search stackoverflow.com for other answers for setting the zoom scale as desired.

here's the code after applying these items.

(void)viewDidLoad {
    self.scrollView.delegate = self;         // can be set in storyboard
    self.scrollView.scrollingEnabled = YES;  // can be set in storyboard
    NSString* bgPng
      = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"];
    UIImage *image1 = [UIImage imageWithContentsOfFile:bgPng];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
//  self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

    // set zoom scale here if desired.

    // if zoom is implemented, the following call should be made
    // after setting .zoomScale, .minimumZoomScale & .maximumZoomScale
    self.scrollView.contentOffset = CGPointZero;

//  [self centerScrollViewContents];
}


//- (void)centerScrollViewContents {
//    CGSize boundsSize = self.scrollView.bounds.size;
//    CGRect contentsFrame = self.imageView.frame;
//
//    if (contentsFrame.size.width < boundsSize.width) {
//        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
//    } else {
//        contentsFrame.origin.x = 0.0f;
//    }
//
//    if (contentsFrame.size.height < boundsSize.height) {
//        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
//    } else {
//        contentsFrame.origin.y = 0.0f;
//    }
//  
//    self.imageView.frame = contentsFrame;
//}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}

Upvotes: 4

dsfdf
dsfdf

Reputation: 106

Did you set the scrollview delegate somewhere else?
Like this: self.scrollView.delegate = self;

Upvotes: 0

darshansonde
darshansonde

Reputation: 501

In the code above, you need to set the minimum and maximum zoom scale.

http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;

Upvotes: 0

Kunal Balani
Kunal Balani

Reputation: 4789

First of all check your scrollview frame . Scrolling will be enabled only when frame size is < contentsize. Because if all the content is visible in frame then why do you need scrolling at all ??

try this :

 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
                 self.view.frame.size.width, self.view.frame.size.height)];
    //init with whatever frame you have

  scroll.scrollEnabled = YES // this is imp ... don't ignore this

   //whatever your code is 

    self.scrollView.contentSize = image.size;// this is wrong

    //contentSize property to the size of the scrollable content. This specifies the size of the scrollable area.
    // do this
    self.scrollView.contentSize = imageView.frame.size;

Upvotes: 1

Srikanth
Srikanth

Reputation: 1930

In the above code, you have provided

    UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

The content size of scrollView which you are setting is equal to the size of the image i.e the above bolded code. Try with the following code.

CGSize sizeOfScroll = image.size;
sizeOfScroll.width *= 1.5f;
sizeOfScroll.height *= 1.5f;

self.scrollView.contentSize = sizeOfScroll;

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107231

Set the user interaction enabled property of image view and scroll view to yes. Please check with this code,

- (void)viewDidLoad
{
    UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    imageView.userInteractionEnabled = YES;
    scrollView.userInteractionEnabled = YES;
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

    [self centerScrollViewContents];
}

And if you need to set the image as your scroll view's background, use this:self.scrollView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]]];

Upvotes: 0

John Smith
John Smith

Reputation: 2032

well, try this :

UIImageView *tempImgv = [[[UIImageView alloc]init] autorelease];
    [tempImgv setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/bg.png", [[NSBundle mainBundle]resourcePath]]]];
    [tempImgv setFrame:your_desired_frame];
    self.imageView = tempImgv;
    [self.scrollWiew setContentSize: size_greater_than_scroll_frame];
    [self.scrollWiew addSubview:self.imageView];

Upvotes: 1

Related Questions