图片加载时滚动卡住
scrolling stuck when image loads
我的图片对于屏幕来说太大了,所以我让它能够向下滚动图片以查看全部内容,但是当我加载我的图片时,我无法在我之前向下滚动'我捏过它。听起来有点奇怪,但我希望有人明白我的意思,并且可以告诉我我做错了什么。
这是我的代码:
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
}
问题是图像是同步加载的,因为它是一个大图像,所以图像完全加载和解码需要时间。那个时候主线程正在使用,滚动事件没有被传递。
您可以加载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
});
});
问题是你在主线程上做这两件事。这意味着您的代码在说 "when I am done loading this image, THEN I can continue scrolling down".
这就是多线程发挥作用的地方,您之前可能已经听说过。我建议你做一些自学并利用 Apple 的文档 here。如果之后您仍然不明白,Whosebug 上还有很多其他示例可供您学习。
哦,永远不要 copy/paste 你从 Whosebug 得到的答案就像你对上一个答案所做的那样。你不会学到任何东西,也不会因此变得更好。 :)
我的图片对于屏幕来说太大了,所以我让它能够向下滚动图片以查看全部内容,但是当我加载我的图片时,我无法在我之前向下滚动'我捏过它。听起来有点奇怪,但我希望有人明白我的意思,并且可以告诉我我做错了什么。
这是我的代码:
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
}
问题是图像是同步加载的,因为它是一个大图像,所以图像完全加载和解码需要时间。那个时候主线程正在使用,滚动事件没有被传递。
您可以加载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
});
});
问题是你在主线程上做这两件事。这意味着您的代码在说 "when I am done loading this image, THEN I can continue scrolling down".
这就是多线程发挥作用的地方,您之前可能已经听说过。我建议你做一些自学并利用 Apple 的文档 here。如果之后您仍然不明白,Whosebug 上还有很多其他示例可供您学习。
哦,永远不要 copy/paste 你从 Whosebug 得到的答案就像你对上一个答案所做的那样。你不会学到任何东西,也不会因此变得更好。 :)