UIScrollView image/photo viewer with paging enabled and zooming

后端 未结 8 860
有刺的猬
有刺的猬 2020-12-07 07:01

OK, I think it\'s time to make an official place on the internet for this problem: How to make a UIScrollView photoviewer with paging and zooming. Welcome my fe

相关标签:
8条回答
  • 2020-12-07 07:33

    Take a look at https://github.com/facebook/three20/blob/master/src/Three20UI/Headers/TTPhotoViewController.h Not sure if that's what you are looking for

    0 讨论(0)
  • 2020-12-07 07:37

    You say you've seen discussions of nesting UIScrollViews but don't want to go there - but that is the way to go! It works easily and well.

    It's essentially what Apple does in its PhotoScroller example (and the 2010 WWDC talk linked to in Jonah's answer). Only in those examples, they've added a whole bunch of complex tiling and other memory management. If you don't need the tiling etc. and if you dont want to wade through those examples and try and remove the bits related to it, the underlying principle of nesting UIScrollViews is actually quite simple:

    • Create an outer UIScrollView and set its pagingEnabled = true. Add it to your main view and set its width and height to your main view's width and height.

    • Create as many inner UIScrollViews as you want images. Set their width and height to your main view's width and height. Add them as subviews to your outer UIScrollView, each one next to the other, left to right.

    • Set the content size of the outer UIScrollView to the total of the widths of all the inner UIScrollViews side by side (which is equal to [your main view's width]*[number of images]).

    • Add your images' UIImageViews to the inner UIScrollViews, one UIImageView to each inner UIScrollView. Set each UIScrollView's content size to each UIImageView's size.

    • Set min and max zoom scales for each inner UIScrollView and set each of the inner UIScrollView's delegate to your View Controller. In the delegate's viewForZoomingInScrollView, return the appropriate UIImageView for the UIScrollView that is passed. (To do this, just keep each of the UIImageViews in an NSArray and set the corresponding UIScrollView's tag property to the index of the appropriate UIImageView. You can then read the tag in the UIScrollView passed to viewForZoomingInScrollView and return the appropriate UIImageView from the NSArray).

    That's it. Works just like the photo app.

    If you have a lot of photos, to save memory you can just have two inner UIScrollViews and two UIImagesViews. You then dynamically flip between them, moving their position within the outer UIScrollView and changing their images as the user scrolls the outer UIScrollView. It's a bit more complex but the same principle.

    0 讨论(0)
  • 2020-12-07 07:39

    I've written a simple and easy to use photo browser called MWPhotoBrowser. I decided to create it as Three20 was too heavy/bloated as all I needed was a photo viewer.

    MWPhotoBrowser can display one or more images by providing either UIImage objects, or URLs to files, web images or library assets. The photo browser handles the downloading and caching of photos from the web seamlessly. Photos can be zoomed and panned, and optional (customisable) captions can be displayed. The browser can also be used to allow the user to select one or more photos using either the grid or main image view.

    0 讨论(0)
  • 2020-12-07 07:46

    I did some playing around with the native Photos app, and I think I can say with confidence they are using a single UIScrollView. The giveaway is this: zoom in on an image, and pull to the left or right. You will see the next or previous photo. If you tug hard enough, it will even page to the next photo at 1.0f zoom. Flip back and the previously zoomed photo will be back to 1.0f zoom as well.

    Obivously I didn't write Photos.app, but I'll take a wild guess at how they did it:

    • A single UIScrollView and a single UIScrollViewDelegate
    • Populate the UIScrollView with UIImageView children
    • Listen for scrollViewDidScroll:
    • Do some math and figure out what page you are currently on
    • Listen for viewForZoomingInScrollView:
    • Return a different view depending on the page index
    • Listen for scrollViewDidEndZooming:withView:atScale: and optionally do some anti-aliasing, etc based on the content

    If you decide to try that out, let me know how it works out for you. I'd love to know how you finally end up getting this to work. Even better, post it to github.

    0 讨论(0)
  • 2020-12-07 07:51

    UPDATE

    I deleted my previous answer because of the news below...

    Big news for those who haven't heard. Apple has released the 2010 WWDC session videos to all members of the iphone developer program. One of the topics discussed is how they created the photos app!!! They build a very similar app step by step and have made all the code available for free.

    It does not use private api either. Here is a link to the sample code download. You will probably need to login to gain access.

    Check This

    And, here is a link to the iTunes WWDC page:

    Check This

    0 讨论(0)
  • 2020-12-07 07:51

    sure would be nice if apple built an image viewer like the photos app into the SDK for us to use. I'm currently using three20 and it works great. But it is a lot of extra stuff to carry around when all you really want is the photo viewer.

    0 讨论(0)
提交回复
热议问题