Reputation: 3
I have a collection view to feed one imageView on the main viewController. I have three problems:
1-I am forced to use "highQualityFormat"in collectionView because I can not get the original image on didSeleect function.is there any way to get the original image?
2- in phAssetCollection we can not use sortDescriptors with creationDate.how can I sort images? startDate & endDate not worked.
3-How can I load the selected image in the first uiviewController instead of using userManager.shared and notificationCenter. Thank you for your attention.
The main viewController:
@IBOutlet weak var frontpic: UIImageView!
@IBAction func openPictureDialog(_ sender: Any) {
if !isLoadSoNew(){return}
selectedPic = 1
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "collectionVc1")//
present(vc,animated: true,completion: nil)
}
@objc func fillFront(_ notification:Notification){
frontpic.contentMode = .scaleAspectFit
frontpic.image = UserManager.shared.collectionViewImage
}
The uicollectionView:
import UIKit
import Photos
class collectionVewControl: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var collectionView1: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView1.delegate = self
collectionView1.dataSource = self
fetchCustomAlbumPhotos()
collectionView1.frame.size.height = view.frame.height-60
}
var imageArray = [UIImage]()
var photo: UIImage? @IBAction func backButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
UserManager.shared.collectionViewImage = imageArray[indexPath.row]
NotificationCenter.default.post(name: Notification.Name(rawValue: "fillFront"), object: nil)
self.dismiss(animated: true, completion: nil)
}
func fetchCustomAlbumPhotos()
{
var albumName = "APKSoft"
var assetCollection = PHAssetCollection()
var photoAssets = PHFetchResult<AnyObject>()
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
// fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let firstObject = collection.firstObject{
//found the album
assetCollection = firstObject
}
else {
albumName = "Recents"
if let firstObject = collection.firstObject{
assetCollection = firstObject
}
}
_ = collection.count
photoAssets = PHAsset.fetchAssets(in: assetCollection, options: nil) as! PHFetchResult<AnyObject>
let imageManager = PHCachingImageManager()
photoAssets.enumerateObjects{(object: AnyObject!,
count: Int,
stop: UnsafeMutablePointer<ObjCBool>) in
if object is PHAsset{
let asset = object as! PHAsset
// print("Inside If object is PHAsset, This is number 1")
let imageSize = CGSize(width: asset.pixelWidth,
height: asset.pixelHeight)
/* For faster performance, and maybe degraded image */
let options = PHImageRequestOptions()
options.deliveryMode = .highQualityFormat
options.isSynchronous = true
imageManager.requestImage(for: asset,
targetSize: imageSize,
contentMode: .aspectFill,
options: options,
resultHandler: {
(image, info) -> Void in
self.photo = image!
/* The image is now available to us */
self.addImgToArray(uploadImage: self.photo!)
// print("enum for image, This is number 2")
})
}
}
}
func addImgToArray(uploadImage:UIImage)
{
self.imageArray.append(uploadImage)
}
}
Upvotes: 0
Views: 171
Reputation: 3
I found the solution for the second part of my own question. I had to use fetchOption for phAsset not phAssetCollection and this was my mistake.
_ = collection.count
fetchOptions1.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
photoAssets = PHAsset.fetchAssets(in: assetCollection, options: fetchOptions1) as! PHFetchResult<AnyObject>
Upvotes: 0
Reputation: 410
Hi I can answer your third question now, I am not sure for the others.
You can use protocol and delegate for passing image to previous view controller.
You can check this Medium post out.
Upvotes: -1