Skip to content

Commit

Permalink
Merge pull request #67 from DeveloperAcademy-POSTECH/RecordDetailView…
Browse files Browse the repository at this point in the history
…/AddActionSheetHeader

[Feat] action sheet header 설명 추가
  • Loading branch information
JMM00 committed Sep 30, 2022
2 parents aa48ade + 8922b98 commit 382771b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
23 changes: 0 additions & 23 deletions Model/Screenshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@ import SwiftUI
// MARK: - 이미지 저장 기능을 위한 확장
extension View {

//화면에 보이는 그대로를 캡쳐
func screenshot() -> UIImage {

var image: UIImage?
let scenes = UIApplication.shared.connectedScenes
let windowScenes = scenes.first as? UIWindowScene
guard let currentView = windowScenes?.windows.first(where: { $0.isKeyWindow })?.layer else { return UIImage() }
let screenScale = UIScreen.main.scale

// image capture
UIGraphicsBeginImageContextWithOptions(currentView.frame.size, false, screenScale)
guard let currentContext = UIGraphicsGetCurrentContext() else { return UIImage() }
currentView.render(in: currentContext)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

// crop image
let cropRect: CGRect = .init(origin: CGPoint(x: 0, y: 250), size: CGSize(width: 1500 , height: 2000))
let imageRef = image!.cgImage!.cropping(to: cropRect);
let newImage = UIImage(cgImage: imageRef!, scale: image!.scale, orientation: image!.imageOrientation)

return newImage
}
//UIView를 캡쳐
func snapShot() -> UIImage {
let controller = UIHostingController(rootView: self)
Expand Down
38 changes: 36 additions & 2 deletions Record/RecordDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import SwiftUI
import CoreData
import LinkPresentation

struct RecordDetailView: View {

Expand Down Expand Up @@ -178,9 +179,9 @@ struct RecordDetailView: View {

}
func actionSheet() {

let shareImage = self.snapShot()
let activitiViewController = UIActivityViewController(activityItems: [shareImage], applicationActivities: nil)
let activityItemMetadata = MyActivityItemSource(text: "\(item.title!) - \(item.artist!)" , image: shareImage)
let activitiViewController = UIActivityViewController(activityItems: [activityItemMetadata, shareImage], applicationActivities: nil)
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.windows.first?.rootViewController?.present(activitiViewController, animated: true, completion: nil)
}
Expand Down Expand Up @@ -246,3 +247,36 @@ struct CDPlayerComp: View {
}
}
}

//출처: https://developer.apple.com/forums/thread/687916
class MyActivityItemSource: NSObject, UIActivityItemSource {
var title: String = "RE:CORD"
var text: String
var image: UIImage

init(text: String, image: UIImage) {
self.text = text
self.image = image
super.init()
}

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return text
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return text
}

func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return title
}

func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = title
metadata.iconProvider = NSItemProvider(object: UIImage(imageLiteralResourceName: "AppIcon") )
metadata.originalURL = URL(fileURLWithPath: text)
return metadata
}
}

0 comments on commit 382771b

Please sign in to comment.