IOS 照片框架
IOS Photos framework
我想从设备上的所有 local 相册中检索所有照片。基本上设备上的所有照片
本地标识符列表是否唯一?
使用照片框架的最佳方法是什么。
我的问题没有重复,因为另一个问题也涉及云资产和不在设备上的资产。检索图像的实际数据时,它 returns 尝试获取同步时的空数据。
PHFetchResult *smartAlbums;
smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (NSInteger i =0; i < smartAlbums.count; i++)
{
@autoreleasepool
{
PHAssetCollection *assetCollection = smartAlbums[i];
//Camera Roll
//All Photos //Moments
NSLog(@"assetCollection.localizedTitle---%@",assetCollection.localizedTitle);
// here you will get camera roll photos
if([assetCollection.localizedTitle isEqualToString:@"Camera Roll"] || [assetCollection.localizedTitle isEqualToString:@"All Photos"] )
{
PHFetchOptions *options1 = [[PHFetchOptions alloc] init];
options1.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options1.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
// here you will get all assets of your camera roll images in assetsFetchResult
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options1];
}
}
}
I want to retrieve all the photos from all the local albums on the
device. Basically all the photos that are on the device
编辑:fetchOptions.includeAssetSourceTypes = .typeUserLibrary 见下方代码
我就是这样做的:
var fetchResult: PHFetchResult<PHAsset>!
...
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.includeAssetSourceTypes = .typeUserLibrary
fetchResult = PHAsset.fetchAssets(with: fetchOptions)
然后如果我想将它用作 UImage 或缩略图(如果你想将图像用作数据,请使用此 let imageData: NSData = UIImagePNGRepresentation(myImage)
)我使用:
/*
* 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
}
您可能会根据需要自定义上述代码或添加更多功能!
以上代码是使用Swift3.1和Xcode8.3.1
编写和测试的
根据 Apple 文档
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.
使用 getAsset() 方法从资产中检索图像。有效
步骤 1.
在您的项目中嵌入 Photos.framework
步骤 2
#import <Photos/Photos.h>
步骤 3
在 plist 上添加 NSPhotoLibraryUsageDescription 键
<key>NSPhotoLibraryUsageDescription</key>
<string>${PRODUCT_NAME} PhotoLibrary Usage</string>
步骤 4
编写此代码以获取 PHFetchResult
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *tempAssetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
tempAssetsFetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
我想从设备上的所有 local 相册中检索所有照片。基本上设备上的所有照片 本地标识符列表是否唯一? 使用照片框架的最佳方法是什么。
我的问题没有重复,因为另一个问题也涉及云资产和不在设备上的资产。检索图像的实际数据时,它 returns 尝试获取同步时的空数据。
PHFetchResult *smartAlbums;
smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (NSInteger i =0; i < smartAlbums.count; i++)
{
@autoreleasepool
{
PHAssetCollection *assetCollection = smartAlbums[i];
//Camera Roll
//All Photos //Moments
NSLog(@"assetCollection.localizedTitle---%@",assetCollection.localizedTitle);
// here you will get camera roll photos
if([assetCollection.localizedTitle isEqualToString:@"Camera Roll"] || [assetCollection.localizedTitle isEqualToString:@"All Photos"] )
{
PHFetchOptions *options1 = [[PHFetchOptions alloc] init];
options1.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options1.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
// here you will get all assets of your camera roll images in assetsFetchResult
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options1];
}
}
}
I want to retrieve all the photos from all the local albums on the device. Basically all the photos that are on the device
编辑:fetchOptions.includeAssetSourceTypes = .typeUserLibrary 见下方代码
我就是这样做的:
var fetchResult: PHFetchResult<PHAsset>!
...
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.includeAssetSourceTypes = .typeUserLibrary
fetchResult = PHAsset.fetchAssets(with: fetchOptions)
然后如果我想将它用作 UImage 或缩略图(如果你想将图像用作数据,请使用此 let imageData: NSData = UIImagePNGRepresentation(myImage)
)我使用:
/*
* 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
}
您可能会根据需要自定义上述代码或添加更多功能!
以上代码是使用Swift3.1和Xcode8.3.1
编写和测试的根据 Apple 文档
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.
使用 getAsset() 方法从资产中检索图像。有效
步骤 1.
在您的项目中嵌入 Photos.framework
步骤 2
#import <Photos/Photos.h>
步骤 3
在 plist 上添加 NSPhotoLibraryUsageDescription 键
<key>NSPhotoLibraryUsageDescription</key>
<string>${PRODUCT_NAME} PhotoLibrary Usage</string>
步骤 4
编写此代码以获取 PHFetchResult
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *tempAssetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
tempAssetsFetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];