Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Photos framework

I want to retrieve all the photos from all the local albums on the device. Basically all the photos that are on the device Will the list of local identifiers be unique ? What is the best approach for this using the photos framework.

My question is not duplicate since the other question talks about also cloud assets and assets that are not on the device. When retrieving actual data of the image it returns null data when trying to fetch synchronize.

like image 271
Noam Segev Avatar asked Mar 30 '17 09:03

Noam Segev


People also ask

What are the iOS development frameworks?

In general, these iOS development frameworks allow iOS app developers to develop an application with the help of various sub-components. These components consist of toolsets, different code libraries, several interfaces, and debuggers. iOS development frameworks are the base of any MacOS mobile application.

What is Apple's vision framework?

Now, the purpose of this article is to present Vision, Apple’s framework for developing computer vision iOS applications. Vision Framework offers a number of image analysis and computer vision capabilities. With it, you can perform: Classification and detection of objects using CoreML models. In Vision, there are three roles:

What are mobile app development frameworks and why are they important?

Fundamentally, mobile app development frameworks are the foundation upon which every single wonderful iOS or Android application is built upon. iOS applications are becoming popular with each passing year. With almost 100 million iPhone users in the US alone, there are a lot of possibilities to reach new eyes across the iOS platform.

What are the best libraries for downloading images on iOS?

It’s one of the top iOS libraries out there because it focuses on one common issue and makes it simple and easy. SDWebImage is a library that provides an asynchronous image downloader with cache support.


1 Answers

I want to retrieve all the photos from all the local albums on the device. Basically all the photos that are on the device

Edit: fetchOptions.includeAssetSourceTypes = .typeUserLibrary see below code

That's how I do it:

var fetchResult: PHFetchResult<PHAsset>!

...

let fetchOptions = PHFetchOptions()     
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.includeAssetSourceTypes = .typeUserLibrary

fetchResult = PHAsset.fetchAssets(with: fetchOptions)

Then if I want to use it as UImage or thumbnail (also if you want the image as Data use this let imageData: NSData = UIImagePNGRepresentation(myImage)) I use:

/* 
 * From an asset to a UIImage, also can be used for thumbnail (if you change the :targetSize:)
 */
func getAsset(_ asset: PHAsset) -> UIImage {

    //var thumbnail = UIImage()
    var imageData = Data()

    let options = PHImageRequestOptions()
    options.isSynchronous = true
    options.deliveryMode = .opportunistic
    options.resizeMode = .fast
    options.isNetworkAccessAllowed = false

    PHImageManager.default().requestImage(for: asset, targetSize: view.frame.size, contentMode: .aspectFill, options: options) { image, info in

      //thumbnail = image!
        imageData: NSData = UIImagePNGRepresentation(image)
    }
    //You can check if the UIImage is nil. If it is nil is an iCloud image
    //return thumbnail
    return imageData
}

You will probably customize the above code or add more features based on your needs!

The above code is written and tested using Swift 3.1 and Xcode 8.3.1

According to Apple's Docs

isNetworkAccessAllowed A Boolean value that specifies whether Photos can download the requested image from iCloud. If true, and the requested image is not stored on the local device, Photos downloads the image from iCloud. To be notified of the download’s progress, use the progressHandler property to provide a block that Photos calls periodically while downloading the image. If false (the default), and the image is not on the local device, the PHImageResultIsInCloudKey value in the result handler’s info dictionary indicates that the image is not available unless you enable network access.

Use the getAsset() method for retrieving the image from the asset. It works

like image 81
George Vardikos Avatar answered Oct 07 '22 03:10

George Vardikos