0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift5でDropbox APIを使いDropbox上にある画像を表示

Last updated at Posted at 2019-12-15

導入まではこちら
Dropbox APIをSwift5で使用する方法

バージョン情報

・Swift version 5.0.1
・Xcode version 10.2.1

ダウンロードボタンを設置

ViewController.swift
import UIKit
import SwiftyDropbox

class ViewController: UIViewController {
    //イメージビューを追加
   let myImageView = UIImageView()

    override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view.
       //省略

       //ダウンロードボタンを追加
       let downloadButton = UIButton(type: UIButton.ButtonType.system)
       downloadButton.frame = CGRect(x: 10, y: 220, width: 100, height: 30)
       downloadButton.setTitle("Download", for: .normal)
       downloadButton.addTarget(self, action: #selector(self.downloadDropboxFile), for: .touchUpInside)
       self.view.addSubview(downloadButton)
       
       //画像表示エリアの記載
       myImageView.frame = CGRect(x: 10, y: 500, width: 200, height: 120)
       self.view.addSubview(myImageView)
   }

ダウンロード 画面表示処理

ViewController.swift
@objc func downloadDropboxFile() {
   //ダウンロード処理
   if let client = DropboxClientsManager.authorizedClient {
       //ダウンロード先URLを設定
       let destination : (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
           let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
           let UUID = Foundation.UUID().uuidString
           var fileName = ""
           if let suggestedFilename = response.suggestedFilename {
               fileName = suggestedFilename
           }
           let pathComponent = "\(UUID)-\(fileName)"
           return directoryURL.appendingPathComponent(pathComponent)
       }
       //画面描画処理
       client.files.download(path: "/logo.png", destination: destination).response { response, error in
           if let (metadata, url) = response {
               print("Downloaded file name: \(metadata.name)")
               do {
                   //urlをData型に変換
                   let data = try Data(contentsOf: url)
                   //Data型に変換したurlをUIImageに変換
                   let img = UIImage(data: data)
                   //UIImageをivに変換
                   let iv:UIImageView = UIImageView(image:img)
                   //変換したivをviewに追加
                   self.view.addSubview(iv)
                   //表示位置決定
                   iv.layer.position = CGPoint(x: self.view.bounds.width/2, y: 400.0)
               } catch let err {
                   print("Error : \(err.localizedDescription)")
               }
           } else {
               print(error!)
           }
       }
   }
}

画面

DropBoxサイト上
picture_pc_22a741f1f9a99d5da49ec7bf8a0f6898.png

入れた画像
picture_pc_029190303d86184deef86de18a6f501a.png

動作画面

ダウンロードボタンを押下すると画像が表示される

参考

Download-style request
[Swift] 指定したURLの画像を表示する。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?