0

I have an image that is too big for the screen, so I have made it able to scroll down on the image to see it all, but when I load my image I am not able to scroll down before I've pinched it. It sounds kind of weird, but I hope someone understand what I meant, and is possible to tell me what I've done wrong.

Here's my code:

class DetailViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var myDetailedImageView: UIImageView!

var myDetailedImageName: String?
var nameString: String?

override func viewDidLoad() {
    super.viewDidLoad()

    func minscale () {

    }
    //For setting max and min zoom
     scrollView.maximumZoomScale = 0.8
    scrollView.minimumZoomScale = 0.15

    scrollView.delegate = self

    //reset zoomzcale for new image
    self.scrollView.zoomScale = 0.15

    self.myDetailedImageView.image = UIImage(named: myDetailedImageName!)
}

@IBAction func actionButton(sender: AnyObject) {
    let firstActivityItem = NSString(string: myDetailedImageName!)

    let activityViewController = UIActivityViewController(activityItems: [firstActivityItem, self.myDetailedImageView.image!], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}

//Showing what to zoom/scroll

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {

    //returning image to reload it self

    return myDetailedImageView
}
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Marius Hegg
  • 99
  • 10

2 Answers2

1

The problem is that you're doing both of these things on the main thread. That means that your code is saying "when I am done loading this image, THEN I can continue scrolling down".

This is where, and you may have heard of this before, multi-threading comes into play. I suggest you do some self-learning and take advantage of Apple's documentation here. If you still don't understand after that, there are plenty of other examples on StackOverflow that you can learn from.

Oh, and don't ever copy/paste your answers from StackOverflow like you were doing with the previous answer. You will learn nothing and won't be better off for it. :)

Zolnoor
  • 699
  • 6
  • 11
  • Thanks for answering! I can't find any examples of how to do this for the swift language, so that is why i'm posting this :) I'll be happy if you know how to do this! – Marius Hegg Mar 23 '15 at 15:33
  • *sigh* http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1 – Zolnoor Mar 23 '15 at 17:28
  • @MariusHegg if this answer helped you in any way, please mark it as accepted so future users will know to follow this advice as well. :) – Zolnoor Apr 15 '15 at 20:07
  • Of course it helped me, I will mark it right away :):) – Marius Hegg Apr 15 '15 at 20:08
0

The problem is that the image is being loaded synchronously, and since it is a big image it takes time for the image to be fully loaded and decoded. In that time the main thread is being used and the events for the scrolling aren't being passed.

You can load your image asynchronously this way:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
    let img = UIImage(named: myDetailedImageName!)
    // Make a trivial (1x1) graphics context, and draw the image into it
    UIGraphicsBeginImageContext(CGSizeMake(1,1))
    let context = UIGraphicsGetCurrentContext()
    CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), img.CGImage)
    UIGraphicsEndImageContext()

    // Now the image will have been loaded and decoded and is ready to rock for the main thread
      dispatch_sync(dispatch_get_main_queue()) {
        self.myDetailedImageView.image = img
    });
});
Community
  • 1
  • 1
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82