Skip to content

Commit

Permalink
Update README.MD about step3
Browse files Browse the repository at this point in the history
Update about develop and studies about step3
  • Loading branch information
JacksonPk authored Mar 26, 2021
1 parent d767334 commit 4eb0691
Showing 1 changed file with 145 additions and 3 deletions.
148 changes: 145 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
let photoAssets = PHAsset.fetchAssets(in: allContents, options: .none)
```

- `PHImageManager` 는 `PHAssets`의 메타데이터 정보를 관리한다. 일반적으로 `PHAssets`의 썸네일 이미지를 가져올 수 있으며, 사이즈를 재정의 할 수 있다.
- `PHImageManager` 는 `PHAssets`의 메타데이터 정보를 관리한다. 일반적으로 `PHAssets`의 썸네일 이미지를 가져올 수 있으며, 사이즈를 재정의 할 수 있다.

- `PHCachingImageManager`은 `PHImageManager`의 하위 클래스이며 대부분의 속성을 그대로 가져온다. 차이점은 한번에 여러 `PHAssets`를 가져오거나 관리를 할 수 있다.
- `PHCachingImageManager`은 `PHImageManager`의 하위 클래스이며 대부분의 속성을 그대로 가져온다. 차이점은 한번에 여러 `PHAssets`를 가져오거나 관리를 할 수 있다.

- 이때, `PHImageRequestOption `객체를 통해 size 등의 옵션을 설정할 수 있다. 이 프로젝트에서는 resizeMode를 설정하여 이미지가 원하는 사이즈대로 표시될 수 있게 했다.

Expand All @@ -104,7 +104,7 @@

---

## Step 3 : JSON파일에서 Image를 ControllerView에 보여주기
## Step 3-1 : JSON파일에서 Image를 ControllerView에 보여주기



Expand Down Expand Up @@ -248,3 +248,145 @@

<img src = "https://user-images.githubusercontent.com/52390975/112326069-8d474100-8cf7-11eb-9303-ee338433ae43.gif" width = 200>



## Step 3-2 : 다운받은 Image를 DoodlesView에서 보여주기



#### 2021.03.25

- ### 구현 내용 📱

- DoodleViews를 StoryBoard 이용 없이 Code로 구현.
- 서로 다른 DispatchQueue를 이용하여 다운받는 큐와 보여주는 큐를 Async하게 작업



### Today's 학습거리 📚

1. DoodleViews를 StoryBoard 이용 없이 DoodleViewsController에서 Code로 구현

- `CollectionView`를 코드로 생성하기 위해 `UICollectionViewFlowLayout`로 옵션 및 구현했다.

- ```swift
lazy private var doodlesCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(ImgViewCell.self, forCellWithReuseIdentifier: cellID)
return cv
}()
```

- `CollectionView`와 그 안에 들어갈 각 Cell, 그리고 ImageViews들을 코드로 `AutoLayout` 설정해주는 방법을 배웠다.

- `Navigation Bar Item`는 특정위치에만 생성할 수 있다는 점과 dismiss()를 통해 View의 전환이 가능한 점을 배웠다.



2. `URL`을 통한 Images를 저장하고 저장된 Images를 View에 보여주는 작업을 Async하게 구현

- `ImageManager`를 통해 전날 작업한 JSON파일을 통해 작업하는 ImageDownload 그리고, DownLoad한 이미지를 콜렉션 뷰에 보여주는 작업을 공부하였다.

- 각각의 주소에 해당하는 이미지를 다운받는 함수

- ```swift
private func startDownloading() {
imageURLs.forEach { (url) in
backGroundDownloadingQueue.async {
self.downloader.downloadImage(imageURL: url, completionHandler: { (_) in },
placeholderImage: UIImage())
}
}
}
```

- 다운받은 이미지를 해당 Cell에 전송하는 방법으로 `Notification`을 활용하여 즉각적으로 보내게 하였다.

- ```swift
NotificationCenter.default.addObserver(self,
selector: #selector(didImageDownloadDone(_:)),
name: ImageManager.NotiKeys.imageDownloadDone,
object: imgManager)
```

- `DispatchQueue`에 대한 `Asyn`에 고찰

- 일반적으로는 각각의 주소에 해당하는 이미지를 다운받도록 한다. 하지만 다음과 같은 상황에 대비를 해야한다.

- 모든 데이터가 다운받기 전까지 이미지 뷰를 보여주는 함수가 실행되지 않는다.
- 이미지를 보여주는 것이 순차적이라 스크롤 뷰를 내렸을 때 나오는 셀에 대한 이미지가 아직 보여지지 않는다.

- 따라서 모든 뷰를 순차적으로 이미지를 받게하고, `dequeueReusableCell` 에 해당하는 새로운 Cell이 보여주었을 때를 대비하여 즉각적인 이미지를 보여주는 각각의 쓰레드를 생성하여 Asyn하게 작업하도록 하였다.

- ```swift
imageDownloadQueue = DispatchQueue.init(label: "imageDownload")
backGroundDownloadingQueue = DispatchQueue.init(label: "backgroundImageDownload")
```



<img src = https://user-images.githubusercontent.com/52390975/112632344-5b5ee780-8e7b-11eb-8396-4f3cccee1424.gif width = 200>



## Step 3-3 : Image를 저장하기



#### 2021.03.26

- ### 구현 내용 📱

- Image를 길게 눌러서 저장하기.
- 저장 시 알림메시지 나오기 및 앨범 갱신.



### Today's 학습거리 📚

1. `UILongPressGestureRecognizer` 를 사용하여 해당 셀을 클릭함에 따라 UIMenuItem이 나오도록 학습하였다.

- ```swift
let menuController = UIMenuController.shared
let save = UIMenuItem(title: "Save", action: #selector(saveImageAtCell))
```

- 선택한 Cell의 Image를 Local Library에 넣는 방법은 `UIImageWriteToSavedPhotosAlbum` 를 사용하였다.

- ```swift
UIImageWriteToSavedPhotosAlbum(cellImage, self, #selector(imageSaveAlert(_:didFinishSavingWithError:contextInfo:)), nil)
```

2. `Save` Btn을 이용하여 저장할 시, 저장이 되었는지를 알려주는 기능이 필요하다고 의논하였다.

- 이유 : DoodlesView에와 LocalAlbumView가 서로 다르기에 실제로 저장되었는지는 View전환을 통해서만 확인이 가능
- **사용성 편의 증진!!**

- `UIAlertController` 을 이용하여 사용자에게 저장 완료 돼었음을 알려줌

- ```swift
let ac = UIAlertController(title: "저장 성공!", message: "잘 하셨습니다.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "", style: .default))
present(ac, animated: true)
```

3. DoodlesView에서 저장한 Images를 LocalAlbumView에서 확인하기.

- **문제점 발생** : 저장된 Image가 LocalAlbumView에서 보여지지 않는 현상발견😢

- 이유 : `modalPresentationStyle` 의 `formSheet`가 default인 상황. 이에 따라 생명주기를 두 Views 모두 공유하게 돼었음. (Modal은 `temporary mode` 의 의미를 가진다.)
- 해결책 : `modalPresentationStyle` 를 .fullScreen 으로 변경함. 뷰 스택에 서로 다른 views가 생김

- LocalAlbumView에서 `ViewWillAppear()`를 통해 갱신된 데이터로 `CollectionView`를 보여준다.

- ```swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.collectionView.reloadData()
}
```

<img src = https://user-images.githubusercontent.com/52390975/112632471-834e4b00-8e7b-11eb-8aa5-29045b828893.gif width = 200>

0 comments on commit 4eb0691

Please sign in to comment.