How do I resize a picture using UIImageView so that I can zoom in and out?

柔情痞子 提交于 2019-12-24 03:47:09

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3339084/how-do-i-resize-a-picture-using-uiimageview-so-that-i-can-zoom-in-and-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!