2

I am writing an app that has a screensaver which traverses the photos from the Camera Roll of the iPad. I am stuck at getting the image file names to be stored into an array so that i can display the image accordingly.

import Photos

@IBOutlet weak var ssImage: UIImageView!
let fetchOptions = PHFetchOptions()
var arrayPhoto: [UIImage] = []

func FetchPhotos() {
    let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)

    //Get the file names of all the photos and store into arrayPhoto
}

//Gets index of current image from array and increment 1 so as to display the next photo every 5 seconds.
func nextImage() {
    let currentIndex = arrayPhoto.index(of: imageView.image ?? UIImage()) ?? -1
    var nextIndex = currentIndex+1 
    nextIndex = arrayPhoto.indices.contains(nextIndex) ? nextIndex : 0
    UIView.transition(with: ssImage, duration: 0.5, options: .transitionCrossDissolve, animations: { self.ssImage.image = self.arrayPhoto[nextIndex] }, completion: nil)
    ssImage.image = arrayPhoto[nextIndex]
}

func scheduledTimer() {
    timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(nextImage)), userInfo: nil, repeats: true)
}

override func viewDidLoad() {
    super.viewDidLoad
    FetchPhotos()
    scheduledTimer()
}

1 Answer 1

1

I got your question. now create function convertAssetToImage

func convertAssetToImage(asset: [PHAsset], completion: (_ objects: [UIImage]) -> Void) {
    var result: [UIImage] = []
    asset.forEach { (item) in

        imageManager.requestImage(for: item, targetSize: CGSize(width: item.pixelWidth, height: item.pixelHeight), contentMode: .aspectFill, options: nil, resultHandler: { image, _ in
            // The cell may have been recycled by the time this handler gets called;
            // set the cell's thumbnail image only if it's still showing the same asset.
            result.append(image!)
        })

    }
    completion(result)
}

than call it with your function

func FetchPhotos() {
  let photoAssets = PHAsset.fetchAssets(with: .image, options: fetchOptions)
  convertAssetToImage(asset: photoAssets, completion: { (images) in
                        arrayPhoto = images
                    })
}

Note: be careful when call FetchPhotos() func to viewDidLoad because func convertAssetToImage is asynchronous func.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, thanks for the reply. Unfortunately, I got the error "Use of unresolved identifier" from convertImageToThumbnail and thumbnailSize.
replace thumbnailSize with CGSize(width: item.pixelWidth, height: item.pixelHeight) see update answer
"imageManager" gives the "Used of unresolved identifier" error so i replaced it with "PHImageManager" but I got another error, "Instance member 'requestImage' cannot be used on type 'PHImageManager' "
"convertImageToThumbnail" gives the error "Use of unresolved identifier"
ok.. just place convertImageToThumbnail to convertAssetToImage . sorry it my bad. see update answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.