开发者

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

In my view I have a pict开发者_开发技巧ure.. 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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜