Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] #216 - 명함 이미지 렌더링해서 앨범에 저장 구현 #222

Merged
merged 3 commits into from
Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions NADA-iOS-forRelease/Sources/Cells/CardCell/BackCardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Kingfisher

class BackCardCell: CardCell {

// MARK: - Properties

private var cardData: Card?

// MARK: - @IBOutlet Properties
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var tasteTitleLabel: UILabel!
Expand All @@ -34,7 +38,7 @@ class BackCardCell: CardCell {
setUI()
}
@IBAction func touchShareButton(_ sender: Any) {
NotificationCenter.default.post(name: Notification.Name.presentCardShare, object: nil, userInfo: nil)
NotificationCenter.default.post(name: Notification.Name.presentCardShare, object: cardData, userInfo: nil)
}

// MARK: - Functions
Expand All @@ -59,60 +63,54 @@ extension BackCardCell {
}

/// 서버에서 image 를 URL 로 가져올 경우 사용.
func initCell(_ backgroundImageString: String,
_ isMint: Bool,
_ isSoju: Bool,
_ isBoomuk: Bool,
_ isSauced: Bool,
_ firstTMI: String,
_ secondTMI: String,
_ thirdTMI: String,
func initCellFromServer(cardData: Card,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔합니다~

isShareable: Bool) {
self.cardData = cardData

if backgroundImageString.hasPrefix("https://") {
self.backgroundImageView.updateServerImage(backgroundImageString)
if cardData.background.hasPrefix("https://") {
self.backgroundImageView.updateServerImage(cardData.background)
} else {
if let bgImage = UIImage(named: backgroundImageString) {
if let bgImage = UIImage(named: cardData.background) {
self.backgroundImageView.image = bgImage
}
}

mintImageView.image = isMint == true ?
mintImageView.image = cardData.isMincho == true ?
UIImage(named: "iconTasteOnMincho") : UIImage(named: "iconTasteOffMincho")
noMintImageView.image = isMint == false ?
noMintImageView.image = cardData.isMincho == false ?
UIImage(named: "iconTasteOnBanmincho") : UIImage(named: "iconTasteOffBanmincho")

sojuImageView.image = isSoju == true ?
sojuImageView.image = cardData.isSoju == true ?
UIImage(named: "iconTasteOnSoju") : UIImage(named: "iconTasteOffSoju")
beerImageView.image = isSoju == false ?
beerImageView.image = cardData.isSoju == false ?
UIImage(named: "iconTasteOnBeer") : UIImage(named: "iconTasteOffBeer")

pourEatImageView.image = isBoomuk == true ?
pourEatImageView.image = cardData.isBoomuk == true ?
UIImage(named: "iconTasteOnBumeok") : UIImage(named: "iconTasteOffBumeok")
putSauceEatImageView.image = isBoomuk == false ?
putSauceEatImageView.image = cardData.isBoomuk == false ?
UIImage(named: "iconTasteOnZzik") : UIImage(named: "iconTasteOffZzik")

sauceChickenImageView.image = isSauced == true ?
sauceChickenImageView.image = cardData.isSauced == true ?
UIImage(named: "iconTasteOnSeasoned") : UIImage(named: "iconTasteOffSeasoned")
friedChickenImageView.image = isSauced == false ?
friedChickenImageView.image = cardData.isSauced == false ?
UIImage(named: "iconTasteOnFried") : UIImage(named: "iconTasteOffFried")

if !firstTMI.isEmpty {
firstTmiLabel.text = "• " + firstTMI
if !cardData.oneTmi.isEmpty {
firstTmiLabel.text = "• " + cardData.oneTmi
} else {
firstTmiLabel.text = firstTMI
firstTmiLabel.text = cardData.oneTmi
}

if !secondTMI.isEmpty {
secondTmiLabel.text = "• " + secondTMI
if !cardData.twoTmi.isEmpty {
secondTmiLabel.text = "• " + cardData.twoTmi
} else {
secondTmiLabel.text = secondTMI
secondTmiLabel.text = cardData.twoTmi
}

if !thirdTMI.isEmpty {
thirdTmiLabel.text = "• " + thirdTMI
if !cardData.threeTmi.isEmpty {
thirdTmiLabel.text = "• " + cardData.threeTmi
} else {
thirdTmiLabel.text = thirdTMI
thirdTmiLabel.text = cardData.threeTmi
}

shareButton.isHidden = !isShareable
Expand Down
42 changes: 20 additions & 22 deletions NADA-iOS-forRelease/Sources/Cells/CardCell/FrontCardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Kingfisher

class FrontCardCell: CardCell {

// MARK: - Properties

private var cardData: Card?

// MARK: - @IBOutlet Properties
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
Expand All @@ -34,7 +38,7 @@ class FrontCardCell: CardCell {
setTapGesture()
}
@IBAction func touchShareButton(_ sender: Any) {
NotificationCenter.default.post(name: Notification.Name.presentCardShare, object: nil, userInfo: nil)
NotificationCenter.default.post(name: Notification.Name.presentCardShare, object: cardData, userInfo: nil)
}

static func nib() -> UINib {
Expand Down Expand Up @@ -105,36 +109,30 @@ extension FrontCardCell {
}

/// 서버에서 image 를 URL 로 가져올 경우 사용.
func initCell(_ backgroundImage: String,
_ cardTitle: String,
_ cardDescription: String,
_ userName: String,
_ birth: String,
_ mbti: String,
_ instagramID: String,
_ linkURL: String,
func initCellFromServer(cardData: Card,
isShareable: Bool) {

if backgroundImage.hasPrefix("https://") {
self.backgroundImageView.updateServerImage(backgroundImage)
self.cardData = cardData

if cardData.background.hasPrefix("https://") {
self.backgroundImageView.updateServerImage(cardData.background)
} else {
if let bgImage = UIImage(named: backgroundImage) {
if let bgImage = UIImage(named: cardData.background) {
self.backgroundImageView.image = bgImage
}
}

titleLabel.text = cardTitle
descriptionLabel.text = cardDescription
userNameLabel.text = userName
birthLabel.text = birth
mbtiLabel.text = mbti
instagramIDLabel.text = instagramID
linkURLLabel.text = linkURL
titleLabel.text = cardData.title
descriptionLabel.text = cardData.cardDescription
userNameLabel.text = cardData.name
birthLabel.text = cardData.birthDate
mbtiLabel.text = cardData.mbti
instagramIDLabel.text = cardData.instagram
linkURLLabel.text = cardData.link

if instagramID.isEmpty {
if cardData.instagram.isEmpty {
instagramImageView.isHidden = true
}
if linkURL.isEmpty {
if cardData.link.isEmpty {
linkURLImageView.isHidden = true
}

Expand Down
30 changes: 3 additions & 27 deletions NADA-iOS-forRelease/Sources/Cells/CardCell/MainCardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ extension MainCardCell {

frontCard.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
guard let cardDataModel = cardDataModel else { return }
frontCard.initCell(cardDataModel.background,
cardDataModel.title,
cardDataModel.cardDescription ?? "",
cardDataModel.name,
cardDataModel.birthDate,
cardDataModel.mbti,
cardDataModel.instagram ?? "",
cardDataModel.link ?? "",
isShareable: isShareable ?? false)
frontCard.initCellFromServer(cardData: cardDataModel, isShareable: isShareable ?? false)

contentView.addSubview(frontCard)
}
Expand All @@ -84,15 +76,7 @@ extension MainCardCell {
guard let backCard = BackCardCell.nib().instantiate(withOwner: self, options: nil).first as? BackCardCell else { return }
backCard.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
guard let cardDataModel = cardDataModel else { return }
backCard.initCell(cardDataModel.background,
cardDataModel.isMincho,
cardDataModel.isSoju,
cardDataModel.isBoomuk,
cardDataModel.isSauced,
cardDataModel.oneTmi ?? "",
cardDataModel.twoTmi ?? "",
cardDataModel.threeTmi ?? "",
isShareable: isShareable ?? false)
backCard.initCellFromServer(cardData: cardDataModel, isShareable: isShareable ?? false)

contentView.addSubview(backCard)
isFront = false
Expand All @@ -101,15 +85,7 @@ extension MainCardCell {

frontCard.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
guard let cardDataModel = cardDataModel else { return }
frontCard.initCell(cardDataModel.background,
cardDataModel.title,
cardDataModel.cardDescription ?? "",
cardDataModel.name,
cardDataModel.birthDate,
cardDataModel.mbti,
cardDataModel.instagram ?? "",
cardDataModel.link ?? "",
isShareable: isShareable ?? false)
frontCard.initCellFromServer(cardData: cardDataModel, isShareable: isShareable ?? false)

contentView.addSubview(frontCard)
isFront = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import UIKit
class CardShareBottomSheetViewController: CommonBottomSheetViewController {

// MARK: - Properties

var cardID: String? = "1D856A"

var isShareable = false
var cardDataModel: Card?

private let qrImage: UIImageView = {
// 여기를 만든 QR이미지로 바꿔주시면 됩니당
let imageView = UIImageView()
Expand Down Expand Up @@ -107,13 +110,82 @@ class CardShareBottomSheetViewController: CommonBottomSheetViewController {
])
}

private func setImageWriteToSavedPhotosAlbum() {
let frontCardImage = setFrontCardImage()
let backCardImage = setBackCardImage()

UIImageWriteToSavedPhotosAlbum(frontCardImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
UIImageWriteToSavedPhotosAlbum(backCardImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
Comment on lines +117 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오... 이거 공부해봐야겠어요 🤩

Copy link
Member Author

@hyun99999 hyun99999 Dec 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거하면서 selector 도 다시금 봤어여 notificationcenter 에서는 selector 의 매개변수로 object 가 넘어왔듯이 이거는 세개를 쓰더라구요!
개발자문서에서 UIImageWriteToSavedPhotosAlbum 의 selector 는 필수요소로 가지는 메서드를 만들어야한다면서 알려주더라구요,..
그냥 들어가서 슥보는것도 괜찮을거 같아여

Apple Developer Documentation

}

// FIXME: - 명함 저장시에도 테두리 둥글게 가능한가 찾기
private func setFrontCardImage() -> UIImage {
guard let frontCard = FrontCardCell.nib().instantiate(withOwner: self, options: nil).first as? FrontCardCell else { return UIImage() }

frontCard.frame = CGRect(x: 0, y: 0, width: 327, height: 540)
guard let cardDataModel = cardDataModel else { return UIImage() }
frontCard.initCell(UIImage(named: cardDataModel.background),
cardDataModel.title,
cardDataModel.cardDescription,
cardDataModel.name,
cardDataModel.birthDate,
cardDataModel.mbti,
cardDataModel.instagram ,
cardDataModel.link,
isShareable: isShareable)

let frontCardView = UIView(frame: CGRect(x: 0, y: 0, width: 327, height: 540))
frontCardView.addSubview(frontCard)

let renderer = UIGraphicsImageRenderer(size: frontCardView.bounds.size)
let frontImage = renderer.image { _ in
frontCardView.drawHierarchy(in: frontCardView.bounds, afterScreenUpdates: true)
}

return frontImage
}
private func setBackCardImage() -> UIImage {
guard let backCard = BackCardCell.nib().instantiate(withOwner: self, options: nil).first as? BackCardCell else { return UIImage() }
backCard.frame = CGRect(x: 0, y: 0, width: 327, height: 540)
guard let cardDataModel = cardDataModel else { return UIImage() }
backCard.initCell(UIImage(named: cardDataModel.background),
cardDataModel.isMincho,
cardDataModel.isSoju,
cardDataModel.isBoomuk,
cardDataModel.isSauced,
cardDataModel.oneTmi,
cardDataModel.twoTmi,
cardDataModel.threeTmi,
isShareable: isShareable)

let backCardView = UIView(frame: CGRect(x: 0, y: 0, width: 327, height: 540))
backCardView.addSubview(backCard)

let renderer = UIGraphicsImageRenderer(size: backCardView.bounds.size)
let backImage = renderer.image { _ in
backCardView.drawHierarchy(in: backCardView.bounds, afterScreenUpdates: true)
}

return backImage
}

// MARK: - @objc Methods

@objc func copyId() {
UIPasteboard.general.string = cardID
showToast(message: "명함 아이디가 복사되었습니다.", font: UIFont.button02, view: "copyID")
}

@objc func saveAsImage() {
showToast(message: "갤러리에 저장되었습니다.", font: UIFont.button02, view: "saveImage")
setImageWriteToSavedPhotosAlbum()
}

@objc
private func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeMutableRawPointer) {
if let error = error {
print(error.localizedDescription)
} else {
showToast(message: "갤러리에 저장되었습니다.", font: UIFont.button02, view: "saveImage")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,8 @@ extension CardDetailViewController {
guard let frontCard = FrontCardCell.nib().instantiate(withOwner: self, options: nil).first as? FrontCardCell else { return }

frontCard.frame = CGRect(x: 0, y: 0, width: cardView.frame.width, height: cardView.frame.height)
frontCard.initCell(cardDataModel?.background ?? "",
cardDataModel?.title ?? "",
cardDataModel?.cardDescription ?? "",
cardDataModel?.name ?? "",
cardDataModel?.birthDate ?? "",
cardDataModel?.mbti ?? "",
cardDataModel?.instagram ?? "",
cardDataModel?.link ?? "",
isShareable: isShareable)
guard let cardDataModel = cardDataModel else { return }
frontCard.initCellFromServer(cardData: cardDataModel, isShareable: isShareable)

cardView.addSubview(frontCard)
}
Expand All @@ -147,31 +140,17 @@ extension CardDetailViewController {
if isFront {
guard let backCard = BackCardCell.nib().instantiate(withOwner: self, options: nil).first as? BackCardCell else { return }
backCard.frame = CGRect(x: 0, y: 0, width: cardView.frame.width, height: cardView.frame.height)
backCard.initCell(cardDataModel?.background ?? "",
cardDataModel?.isMincho ?? true,
cardDataModel?.isSoju ?? true,
cardDataModel?.isBoomuk ?? true,
cardDataModel?.isSauced ?? true,
cardDataModel?.oneTmi ?? "",
cardDataModel?.twoTmi ?? "",
cardDataModel?.threeTmi ?? "",
isShareable: isShareable)
guard let cardDataModel = cardDataModel else { return }
backCard.initCellFromServer(cardData: cardDataModel, isShareable: isShareable)

cardView.addSubview(backCard)
isFront = false
} else {
guard let frontCard = FrontCardCell.nib().instantiate(withOwner: self, options: nil).first as? FrontCardCell else { return }

frontCard.frame = CGRect(x: 0, y: 0, width: cardView.frame.width, height: cardView.frame.height)
frontCard.initCell(cardDataModel?.background ?? "",
cardDataModel?.title ?? "",
cardDataModel?.cardDescription ?? "",
cardDataModel?.name ?? "",
cardDataModel?.birthDate ?? "",
cardDataModel?.mbti ?? "",
cardDataModel?.instagram ?? "",
cardDataModel?.link ?? "",
isShareable: isShareable)
guard let cardDataModel = cardDataModel else { return }
frontCard.initCellFromServer(cardData: cardDataModel, isShareable: isShareable)

cardView.addSubview(frontCard)
isFront = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ extension FrontViewController {
}

private func setNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(didRecievePresentCardShare(_:)), name: Notification.Name.presentCardShare, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didRecievePresentCardShare(_:)), name: .presentCardShare, object: nil)
}

@objc func didRecievePresentCardShare(_ notification: Notification) {
let nextVC = CardShareBottomSheetViewController()
.setTitle("명함공유")
.setHeight(404)

if let cardData = notification.object as? Card {
nextVC.cardDataModel = cardData
}

nextVC.modalPresentationStyle = .overFullScreen
self.present(nextVC, animated: false, completion: nil)
}
Expand Down