UIImage does not browse iPhone

I'm new to the iPhone developer,

I want to scroll the image,

Here is my piece of 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};
    [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.pngdoes not apply to my background.

Any help would be appreciated.

+5
source share
8 answers

The answer is actually a combination of many other answers and comments, as well as some other settings.

  • As Pan said, you need to set the scrollView delegate. otherwise, viewForZoomingInScrollView: the delegate implementation will not be called. I provided this call in viewDidLoad: although you can also connect this in an interface constructor or storyboard.
  • , , .imageView.size .scrollView.contentsSize . , .imageView.frame., initWithImage:. .
  • .scrollView.contentSize image.size. , , , , .
  • , scrollingEnabled, .
  • MidhunMP, .userInteractionEnabled = YES, , , /Interface Builder, .
  • centerScrollViewContents imageView. , , . .
  • , zoomScale, minimumZoomScale maximumZoomScale . , .
  • , , . (.. , bg.png , ), contentFrame , , zoomScale, . , , . stackoverflow.com .

.

(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;
}
+4

, :

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];
+1

, . , < contentsize. , , ??

:

 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;

+1

"". , ,

- (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];
}

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

0

    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;

scrollView, , , . .

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

self.scrollView.contentSize = sizeOfScroll;
0

scroll.userinteractionEnable = YES;

,

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

scrollview - ?
: self.scrollView.delegate = self;

0

All Articles