How to set the image fit to the screen after zooming in a scroll view for assignment 4?

166 views
Skip to first unread message

Pan

unread,
Jul 4, 2012, 8:10:08 PM7/4/12
to iphone-appd...@googlegroups.com
Here is my code:

    #import "PhotoViewController.h"
    #import "FlickrFetcher.h"
    
    @interface PhotoViewController () <UIScrollViewDelegate>
    
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (nonatomic,strong) UIImage *image;
    @property (nonatomic,strong) NSDictionary *photo;
    
    @end
    
    @implementation PhotoViewController
    @synthesize imageView = _imageView;
    @synthesize scrollView = _scrollView;
    @synthesize image = _image;
    @synthesize url = _url;
    @synthesize photo = _photo;
    
    - (void)setUrl:(NSURL *)url
    {
        if(_url != url) {
            _url = url;
            NSData *data = [NSData dataWithContentsOfURL:self.url];
            self.image = [UIImage imageWithData:data]; 
        }
    }
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return self.imageView;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    //self.imageView = [self.imageView initWithImage:self.image];
        self.imageView.image = self.image;
        self.scrollView.contentSize = self.imageView.image.size;
        self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
        /*[self.scrollView setContentMode:UIViewContentModeScaleAspectFit];
        [self.scrollView sizeToFit];
        [self.scrollView setContentSize:CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height)]; */
        self.title = [self.photo objectForKey:FLICKR_PHOTO_TITLE];
        self.scrollView.delegate = self;
    }
    
    - (void)viewDidUnload
    {
        [self setImageView:nil];
        [self setScrollView:nil];
        [super viewDidUnload];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }
    
    @end

I'm trying to set the image fit into whole iphone screen size or the UIView size after i zoom it in or out. However, it turns out that the after zooming image always stays in the top left corner of the screen so it leaves much unused white space. I tried to add some code which is the comment ones or change the springs and struts in the storyboard, but all didn't work. 

Another question is i implement the (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation method, but the image didn't rotate as the device rotates, i'm confused.

Hope someone could give me some hints, thanks in advance.
Reply all
Reply to author
Forward
0 new messages