How to get only images in the camera roll using Photos Framework
Through some experimentation we discovered a hidden property not listed in the documentation (assetSource
). Basically you have to do a regular fetch request, then use a predicate to filter the ones from the camera roll. This value should be 3.
Sample code:
//fetch all assets, then sub fetch only the range we need
var assets = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
assets.enumerateObjectsUsingBlock { (obj, idx, bool) -> Void in
results.addObject(obj)
}
var cameraRollAssets = results.filteredArrayUsingPredicate(NSPredicate(format: "assetSource == %@", argumentArray: [3]))
results = NSMutableArray(array: cameraRollAssets)
Fetch only Live Photos using Photos Framework dosen't work
You can't supply a Swift enumeration value to that method - it needs a long (as is implied by the %ld
format string), so use
fetchOptions.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.PhotoLive.rawValue)
to access the underlying integer value of the enumeration value
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
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
Related Topics
Add a Uiview Above All, Even the Navigation Bar
Changing Placeholder Text Color with Swift
How to Call Presentviewcontroller from Within a Uicollectionviewcell
Error: Error Domain=Nsurlerrordomain Code=-1001 "The Request Timed Out."
Programmatically Scroll a Uiscrollview
Libraries Not Found When Using Cocoapods with iOS Logic Tests
Clear Background for Form Sections in Swiftui
Auto Login Dropbox Account on Core API Without Login Prompt
Synchronous Url Request on Swift 2
How to Run Nstimer in Background Beyond 180Sec in iOS 7
How to Add a Button to the Mkpointannotation
How to Create a Big, Red Uibutton with iOS
Firebase with Swift 3 Counting the Number of Children
Add "Edit in Excel" or "Edit Photo" Extension
Right Way of Determining Internet Speed in iOS 8