How do I resize a picture using UIImageView so tha

2019-07-15 13:04发布

In my view I have a picture.. The picture has a 3:2 scale and is huge, resolution wise.

I'm having a lot of trouble initializing a UIImage with the image to the original size and then zooming out of the UIImageView so that the entire image is visible within the scrollview.

The program stays only in the portrait orientation.

And no, please don't say to just use UIWebView. UIWebView doesn't let me set the content size during view load...instead, it just zooms out of the image by some arbitrary scale and I couldn't figure out a way to adjust the scale value (I don't think it's possible).

Thanks for any responses! I really appreciate them :D

3条回答
三岁会撩人
2楼-- · 2019-07-15 13:27

Go to the attributes tab of the UIImageView. Select "aspect to fill" or "aspect to fit". If you use "aspect to fill", you might not show the whole image. I just use "aspect to fit", and make the background black.

查看更多
劳资没心,怎么记你
3楼-- · 2019-07-15 13:36

Here's an example of placing an image that responds to pinch-to-zoom. Basically, you place the UIImageView in a UIScrollView and change some settings.

UIImageView *myImage;
UIScrollView *myScroll;

-(void)viewDidLoad{

  myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,360,450)];
  [myImage setImage:[UIImage imageNamed:@"coolpic.png"]];
  myImage.contentMode = UIViewContentModeScaleAspectFit;
  [myScroll addSubview:myImage];
  [myScroll setContentSize:CGSizeMake(myImage.frame.size.width, myImage.frame.size.height)];
  [myScroll setMinimumZoomScale:1.0];
  [myScroll setMaximumZoomScale:4.0];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return myImage;
}

Of course, set all your delegates and IB hooks properly.

Edit: I just reread your question. The portion of the example that answers your question is the frame specification of the UIImageView and its contentMode setting.

查看更多
冷血范
4楼-- · 2019-07-15 13:48

You almost certainly don't want to load your 'huge, resolution wise' image all at once on load regardless of it's scale. I'd suggest checking out some of Apple's sample code on this stuff (starting with ScrollViewSuite would be good, I'd say).

There was also a recent video released from WWDC where they implement this sort of thing live (they have more of a photo viewing app, but you could pretty easily do what they show with just one image too) so take a look for that too.

查看更多
登录 后发表回答