facebook twitter hatena line email

「Ios/swift/画像ビューアー」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(画像ビューアーサンプル)
行5: 行5:
 
  import SDWebImage
 
  import SDWebImage
 
  class ImageDetailViewController: UIViewController, UIScrollViewDelegate  {
 
  class ImageDetailViewController: UIViewController, UIScrollViewDelegate  {
     @IBOutlet weak var myScrollView: UIScrollView!
+
     @IBOutlet weak var _scrollView: UIScrollView!
     @IBOutlet weak var myImageView: UIImageView!
+
     @IBOutlet weak var _imageView: UIImageView!
 
     internal var imgpath: String! = "https://pbs.twimg.com/media/CnHuxaAWAAI0r3y.jpg"
 
     internal var imgpath: String! = "https://pbs.twimg.com/media/CnHuxaAWAAI0r3y.jpg"
 
     override func viewDidLoad() {
 
     override func viewDidLoad() {
行12: 行12:
 
         if imgpath.containsString("http://") || imgpath.containsString("https://") {
 
         if imgpath.containsString("http://") || imgpath.containsString("https://") {
 
             let imageURL = NSURL(string: imgpath)
 
             let imageURL = NSURL(string: imgpath)
             myImageView.sd_setImageWithURL(imageURL)
+
             _imageView.sd_setImageWithURL(imageURL)
 
         } else {
 
         } else {
             myImageView.image = UIImage(named:imgpath)
+
             _imageView.image = UIImage(named:imgpath)
 
         }
 
         }
 
         // 初期scale
 
         // 初期scale
 
         let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size
 
         let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size
         let zoomScaleWidth:CGFloat = CGFloat(myImageView.bounds.size.width) / CGFloat(myBoundSize.width)
+
         let zoomScaleWidth:CGFloat = CGFloat(_imageView.bounds.size.width) / CGFloat(myBoundSize.width)
         let zoomScaleHeight:CGFloat = CGFloat(myImageView.bounds.size.height) / CGFloat(myBoundSize.height)
+
         let zoomScaleHeight:CGFloat = CGFloat(_imageView.bounds.size.height) / CGFloat(myBoundSize.height)
 
         var zoomScale:CGFloat = zoomScaleWidth
 
         var zoomScale:CGFloat = zoomScaleWidth
 
         if zoomScaleWidth < zoomScaleHeight {
 
         if zoomScaleWidth < zoomScaleHeight {
 
             zoomScale = zoomScaleHeight
 
             zoomScale = zoomScaleHeight
 
         }
 
         }
         self.myScrollView.delegate = self
+
         self._scrollView.delegate = self
         self.myScrollView.minimumZoomScale = 0.1
+
         self._scrollView.minimumZoomScale = 0.1
         self.myScrollView.maximumZoomScale = 8
+
         self._scrollView.maximumZoomScale = 8
         self.myScrollView.scrollEnabled = true
+
         self._scrollView.scrollEnabled = true
         self.myScrollView.showsHorizontalScrollIndicator = true
+
         self._scrollView.showsHorizontalScrollIndicator = true
         self.myScrollView.showsVerticalScrollIndicator = true
+
         self._scrollView.showsVerticalScrollIndicator = true
 
         let doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self
 
         let doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self
 
             , action:#selector(self.doubleTap(_:)))
 
             , action:#selector(self.doubleTap(_:)))
 
         doubleTapGesture.numberOfTapsRequired = 2
 
         doubleTapGesture.numberOfTapsRequired = 2
         self.myImageView.userInteractionEnabled = true
+
         self._imageView.userInteractionEnabled = true
         self.myImageView.addGestureRecognizer(doubleTapGesture)
+
         self._imageView.addGestureRecognizer(doubleTapGesture)
         self.myScrollView.setZoomScale(zoomScale, animated: false)
+
         self._scrollView.setZoomScale(zoomScale, animated: false)
 
     }
 
     }
 
     override func didReceiveMemoryWarning() {
 
     override func didReceiveMemoryWarning() {
行42: 行42:
 
     // ピンチイン・ピンチアウト
 
     // ピンチイン・ピンチアウト
 
     func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
 
     func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
         return self.myImageView
+
         return self._imageView
 
     }
 
     }
 
     // ダブルタップ
 
     // ダブルタップ
 
     func doubleTap(gesture: UITapGestureRecognizer) -> Void {
 
     func doubleTap(gesture: UITapGestureRecognizer) -> Void {
         print(self.myScrollView.zoomScale)
+
         print(self._scrollView.zoomScale)
         if ( self.myScrollView.zoomScale < self.myScrollView.maximumZoomScale ) {
+
         if ( self._scrollView.zoomScale < self._scrollView.maximumZoomScale ) {
             let newScale:CGFloat = self.myScrollView.zoomScale * 3
+
             let newScale:CGFloat = self._scrollView.zoomScale * 3
 
             let zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view))
 
             let zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view))
             self.myScrollView.zoomToRect(zoomRect, animated: true)
+
             self._scrollView.zoomToRect(zoomRect, animated: true)
 
         } else {
 
         } else {
             self.myScrollView.setZoomScale(1.0, animated: true)
+
             self._scrollView.setZoomScale(1.0, animated: true)
 
         }
 
         }
 
     }
 
     }
行58: 行58:
 
     func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{
 
     func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{
 
         var zoomRect: CGRect = CGRect()
 
         var zoomRect: CGRect = CGRect()
         zoomRect.size.height = self.myScrollView.frame.size.height / scale
+
         zoomRect.size.height = self._scrollView.frame.size.height / scale
         zoomRect.size.width = self.myScrollView.frame.size.width / scale
+
         zoomRect.size.width = self._scrollView.frame.size.width / scale
 
         zoomRect.origin.x = center.x - zoomRect.size.width / 2.0
 
         zoomRect.origin.x = center.x - zoomRect.size.width / 2.0
 
         zoomRect.origin.y = center.y - zoomRect.size.height / 2.0
 
         zoomRect.origin.y = center.y - zoomRect.size.height / 2.0

2016年7月30日 (土) 18:15時点における版

画像ビューアーサンプル

画像urlを指定したサンプル

import UIKit
import SDWebImage
class ImageDetailViewController: UIViewController, UIScrollViewDelegate  {
   @IBOutlet weak var _scrollView: UIScrollView!
   @IBOutlet weak var _imageView: UIImageView!
   internal var imgpath: String! = "https://pbs.twimg.com/media/CnHuxaAWAAI0r3y.jpg"
   override func viewDidLoad() {
       super.viewDidLoad()
       if imgpath.containsString("http://") || imgpath.containsString("https://") {
           let imageURL = NSURL(string: imgpath)
           _imageView.sd_setImageWithURL(imageURL)
       } else {
           _imageView.image = UIImage(named:imgpath)
       }
       // 初期scale
       let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size
       let zoomScaleWidth:CGFloat = CGFloat(_imageView.bounds.size.width) / CGFloat(myBoundSize.width)
       let zoomScaleHeight:CGFloat = CGFloat(_imageView.bounds.size.height) / CGFloat(myBoundSize.height)
       var zoomScale:CGFloat = zoomScaleWidth
       if zoomScaleWidth < zoomScaleHeight {
           zoomScale = zoomScaleHeight
       }
       self._scrollView.delegate = self
       self._scrollView.minimumZoomScale = 0.1
       self._scrollView.maximumZoomScale = 8
       self._scrollView.scrollEnabled = true
       self._scrollView.showsHorizontalScrollIndicator = true
       self._scrollView.showsVerticalScrollIndicator = true
       let doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self
           , action:#selector(self.doubleTap(_:)))
       doubleTapGesture.numberOfTapsRequired = 2
       self._imageView.userInteractionEnabled = true
       self._imageView.addGestureRecognizer(doubleTapGesture)
       self._scrollView.setZoomScale(zoomScale, animated: false)
   }
   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
   }
   // ピンチイン・ピンチアウト
   func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
       return self._imageView
   }
   // ダブルタップ
   func doubleTap(gesture: UITapGestureRecognizer) -> Void {
       print(self._scrollView.zoomScale)
       if ( self._scrollView.zoomScale < self._scrollView.maximumZoomScale ) {
           let newScale:CGFloat = self._scrollView.zoomScale * 3
           let zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view))
           self._scrollView.zoomToRect(zoomRect, animated: true)
       } else {
           self._scrollView.setZoomScale(1.0, animated: true)
       }
   }
   // 領域
   func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{
       var zoomRect: CGRect = CGRect()
       zoomRect.size.height = self._scrollView.frame.size.height / scale
       zoomRect.size.width = self._scrollView.frame.size.width / scale
       zoomRect.origin.x = center.x - zoomRect.size.width / 2.0
       zoomRect.origin.y = center.y - zoomRect.size.height / 2.0
       return zoomRect
   }
}

参考:http://qiita.com/hanoopy/items/7a2c582cd9758e7a3076