もし分かる方がいらっしゃいましたら、教えていただきたいです。
私は今、システムアーキテクチャをAWSに依存したiOSモバイルアプリを趣味で開発しています。
写真投稿型SNS(Instagram)のようなものを作ってみたいと思い、
投稿する画像をAWSS3に格納し、それに紐付けたメタデータをAWSDynamoDBに保存しています。
投稿した画像を、例えばタイムラインを見れるようにするために、AWSDynamoDBからメタデータを取ってきて、
(そのメタデータの中に画像のkeyも含まれています)画像をダウンロードして、UITableViewの各セルに表示しているのですが、画像が表示されるまでの時間が遅かったり、同じ画像がいくつも表示されることがあります。
swift
1 2 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 3 let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineTableViewCell", for: indexPath) as! TimelineTableViewCell 4 5 cell.PostID = list[indexPath.row].id 6 cell.CognitoID = list[indexPath.row].CognitoID 7 cell.UserNameLabel.text = list[indexPath.row].CognitoUserName 8 cell.ProfileImageName = list[indexPath.row].ProfileImage 9 cell.PostTextView.text = list[indexPath.row].PostComment 10 cell.YearAndDate = list[indexPath.row].PostedDate 11 cell.Time = list[indexPath.row].PoetedTime 12 cell.LikeCountLabel.text = String(list[indexPath.row].LikeCount) 13 cell.CommentCountLabel.text = String(list[indexPath.row].CommentCount) 14 15 16 cell.ProfileImageButton.tag = indexPath.row 17 cell.ProfileImageButton.addTarget(self, action: #selector(ProfileImageButton(_:)), for: .touchUpInside) 18 19 /* cell.LikeButton.tag = indexPath.row 20 cell.LikeButton.addTarget(self,action: #selector(LikeButtonAction(_:)), for: .touchUpInside) */ 21 22 23 let expressionPost = AWSS3TransferUtilityDownloadExpression() 24 expressionPost.progressBlock = {(task, progress) in 25 DispatchQueue.main.async(execute: { 26 }) 27 } 28 29 transferUtilityPost.downloadData( 30 forKey: cell.CognitoID + cell.YearAndDate + cell.Time, 31 expression: expressionPost, 32 completionHandler: completionHandlerPost).continueWith { (task) -> AnyObject? in 33 if let error = task.error { 34 NSLog("Error: %@",error.localizedDescription); 35 DispatchQueue.main.async(execute: { 36 }) 37 } 38 if let _ = task.result { 39 DispatchQueue.main.async(execute: { 40 }) 41 NSLog("Download Starting!") 42 } 43 return nil; 44 } 45 46 self.completionHandlerPost = { (task, location, data, error) -> Void in 47 DispatchQueue.main.async(execute: { 48 if let error = error { 49 NSLog("Failed with error: (error)") 50 } 51 else{ 52 cell.PostImage.image = UIImage(data: data!) 53 } 54 }) 55 } 56 57 let expressionPostPro = AWSS3TransferUtilityDownloadExpression() 58 expressionPostPro.progressBlock = {(task, progress) in 59 DispatchQueue.main.async(execute: { 60 }) 61 } 62 63 transferUtilityPostPro.downloadData( 64 forKey: cell.ProfileImageName, 65 expression: expressionPostPro, 66 completionHandler: completionHandlerPostPro).continueWith { (task) -> AnyObject? in 67 if let error = task.error { 68 NSLog("Error: %@",error.localizedDescription); 69 DispatchQueue.main.async(execute: { 70 }) 71 } 72 if let _ = task.result { 73 DispatchQueue.main.async(execute: { 74 }) 75 NSLog("Download Starting!") 76 } 77 return nil; 78 } 79 80 self.completionHandlerPostPro = { (task, location, data, error) -> Void in 81 DispatchQueue.main.async(execute: { 82 if let error = error { 83 NSLog("Failed with error: (error)") 84 } 85 else{ 86 cell.ProfileImage.image = UIImage(data: data!) 87 } 88 }) 89 } 90 91 return cell 92 93 }
該当のソースコードは上記の通りなのですが問題点として推測しているのが、
「そもそも上記のコードがうまく表示されない原因を作っている」 (swiftの関数や書き方次第で問題解決する)
「画像ダウンロードに関して、もっとうまい方法がある」(システムのバックエンドをもう一度見直す必要がある)
のどちらかだと考えています。
画像がうまく表示されないのは、画像ダウンロードの遅さゆえと疑い、試しに高速配信をさせてみようと思い、
AWSCloudFrontを用いようと思いましたが、モバイルに配信する場合のやり方もよくわかりません。
もしお詳しい方がいらっしゃいましたら、教えていただきたいです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー