Skip to content

Commit

Permalink
Merge pull request #11 from DeveloperAcademy-POSTECH/soi
Browse files Browse the repository at this point in the history
Feat: 음악 검색 기능, 이미지 저장 기능
  • Loading branch information
YunSeok-Choi committed Jun 16, 2022
2 parents d046d5e + dcdacfe commit dc18508
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 131 deletions.
23 changes: 16 additions & 7 deletions Model/MusicAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,39 @@

import SwiftUI

// MARK: 음악 API 불러오기
class MusicAPI: ObservableObject {
@Published var musicList: [Music] = []

func getSearchResults(search: String) {

self.musicList = []
guard var urlComponents = URLComponents(string: "https://itunes.apple.com/search") else { return }


// URLSession의 싱글톤 객체
guard var urlComponents = URLComponents(string: "https://itunes.apple.com/kr/search") else { return }
urlComponents.query = "media=music&entity=song&term=\(search)"

guard let url = urlComponents.url else { return }

// Networking 시작
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let e = error {
NSLog("error: \(e.localizedDescription)")
return
}

// 값 받아오기
DispatchQueue.main.async() {
do {

// JSON Parsing
let object = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary

guard let jsonObject = object else { return }

let searchResults = jsonObject["results"] as! [NSDictionary]
searchResults.forEach { result in

// Music 객체에 저장
let searchResult = Music(artist: result["artistName"] as! String,
title: result["trackName"] as! String,
albumArt: result["artworkUrl100"] as! String)
Expand All @@ -45,13 +51,16 @@ class MusicAPI: ObservableObject {
} catch let e as NSError {
print("error: \(e.localizedDescription)")
}
}
}

} // DispatchQueue End

} // task End
task.resume()
}
}


// MARK: 음악 정보 저장 구조체
struct Music: Hashable, Codable {
var artist: String // 가수
var title: String // 제목
Expand Down
35 changes: 35 additions & 0 deletions Model/Screenshot.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Screenshot.swift
// Recorder
//
// Created by 이지원 on 2022/06/15.
//

import SwiftUI

// 이미지 저장 기능을 위한 확장
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
}
}
8 changes: 4 additions & 4 deletions Recorder.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
13C0E4A02857267D004ABE1D /* RecordResultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C0E49F2857267D004ABE1D /* RecordResultView.swift */; };
13C0E4A228573795004ABE1D /* RecordDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C0E4A128573795004ABE1D /* RecordDetailView.swift */; };
13E0DAA0285A3764008714A6 /* RecordUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13E0DA9F285A3764008714A6 /* RecordUIView.swift */; };
794AD0072851EEA700414667 /* RecorderApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794AD0062851EEA700414667 /* RecorderApp.swift */; };
Expand All @@ -19,12 +18,12 @@
8715A79D2855B5AC001AB4F5 /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8715A79C2855B5AC001AB4F5 /* Colors.xcassets */; };
8715A79F2855B775001AB4F5 /* Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8715A79E2855B775001AB4F5 /* Extension.swift */; };
876BC4A32857189D009ACC90 /* TestSoiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 876BC4A22857189D009ACC90 /* TestSoiView.swift */; };
87FB281D285A04680036A5BB /* Screenshot.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87FB281C285A04680036A5BB /* Screenshot.swift */; };
A01413222855F4E60000EA59 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A01413212855F4E60000EA59 /* HomeView.swift */; };
A0F567F128563066008F2D2E /* ListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0F567F028563066008F2D2E /* ListView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
13C0E49F2857267D004ABE1D /* RecordResultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordResultView.swift; sourceTree = "<group>"; };
13C0E4A128573795004ABE1D /* RecordDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordDetailView.swift; sourceTree = "<group>"; };
13E0DA9F285A3764008714A6 /* RecordUIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordUIView.swift; sourceTree = "<group>"; };
794AD0032851EEA700414667 /* Recorder.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Recorder.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -37,6 +36,7 @@
8715A79C2855B5AC001AB4F5 /* Colors.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Colors.xcassets; sourceTree = "<group>"; };
8715A79E2855B775001AB4F5 /* Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extension.swift; sourceTree = "<group>"; };
876BC4A22857189D009ACC90 /* TestSoiView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSoiView.swift; sourceTree = "<group>"; };
87FB281C285A04680036A5BB /* Screenshot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Screenshot.swift; sourceTree = "<group>"; };
A01413212855F4E60000EA59 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
A0F567F028563066008F2D2E /* ListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -79,7 +79,6 @@
A01413212855F4E60000EA59 /* HomeView.swift */,
A0F567F028563066008F2D2E /* ListView.swift */,
876BC4A22857189D009ACC90 /* TestSoiView.swift */,
13C0E49F2857267D004ABE1D /* RecordResultView.swift */,
13C0E4A128573795004ABE1D /* RecordDetailView.swift */,
794AD00A2851EEA900414667 /* Assets.xcassets */,
794AD00C2851EEA900414667 /* Preview Content */,
Expand All @@ -101,6 +100,7 @@
isa = PBXGroup;
children = (
8715A79A2855479E001AB4F5 /* MusicAPI.swift */,
87FB281C285A04680036A5BB /* Screenshot.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -176,8 +176,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
13C0E4A02857267D004ABE1D /* RecordResultView.swift in Sources */,
8715A79F2855B775001AB4F5 /* Extension.swift in Sources */,
87FB281D285A04680036A5BB /* Screenshot.swift in Sources */,
8715A79B2855479E001AB4F5 /* MusicAPI.swift in Sources */,
13E0DAA0285A3764008714A6 /* RecordUIView.swift in Sources */,
876BC4A32857189D009ACC90 /* TestSoiView.swift in Sources */,
Expand Down
10 changes: 9 additions & 1 deletion Recorder/RecordDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ struct RecordDetailView: View {
Button(action: {}) { // TODO: antion내에 편집 기능 예정
Label("수정", systemImage: "pencil")
}
Button(action: {}) { // TODO: Soi코딩 중인 스크린샷 기능 예정

// MARK: 이미지 저장 기능
Button(action: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
let image = body.screenshot()
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}) {
Label("이미지로 저장", systemImage: "square.and.arrow.down")
}

Button(role: .destructive, action: {}) { // TODO: action에 삭제 Alert띄우기 및 삭제 기능 예정
Label("삭제", systemImage: "trash")
}
Expand Down
45 changes: 0 additions & 45 deletions Recorder/RecordResultView.swift

This file was deleted.

Loading

0 comments on commit dc18508

Please sign in to comment.