my PHAssetのサムネイル画像を取得したいと思います。私はすでにPhoto LibraryからPHAssetを抽出して、Thumbnail Imageを取得したいと思っています。PHAssetからサムネイル画像を取得
Objective-Cでお手伝いできますか?
ありがとうございます!
my PHAssetのサムネイル画像を取得したいと思います。私はすでにPhoto LibraryからPHAssetを抽出して、Thumbnail Imageを取得したいと思っています。PHAssetからサムネイル画像を取得
Objective-Cでお手伝いできますか?
ありがとうございます!
PHImageManagerClass
は方法があります。
- requestImageForAsset:targetSize:contentMode:options:resultHandler:
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
NSInteger retinaMultiplier = [UIScreen mainScreen].scale;
CGSize retinaSquare = CGSizeMake(imageView.bounds.size.width * retinaMultiplier, imageView.bounds.size.height * retinaMultiplier);
[[PHImageManager defaultManager]
requestImageForAsset:(PHAsset *)_asset
targetSize:retinaSquare
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:^(UIImage *result, NSDictionary *info) {
imageView.image =[UIImage imageWithCGImage:result.CGImage scale:retinaMultiplier orientation:result.imageOrientation];
}];
"このエラーが発生しましたキャッチされていない例外 'NSInvalidArgumentException'によりアプリを終了しています - 理由: ' - [PHAssetCollection isCloudSharedAsset] – Peter
からこの答えを得ることに対する機能&コールを示すスウィフト4のための完全な答えです。また、plistに写真のプライバシーフラグが設定されていることを確認してください。
import Photos
func requestImage(for asset: PHAsset,
targetSize: CGSize,
contentMode: PHImageContentMode,
completionHandler: @escaping (UIImage?) ->()) {
let imageManager = PHImageManager()
imageManager.requestImage(for: asset,
targetSize: targetSize,
contentMode: contentMode,
options: nil) { (image, _) in
completionHandler(image)
}
}
let asset = // your existing PHAsset
let targetSize = CGSize(width: 100, height: 100)
let contentModel = PHImageContentMode.aspectFit
requestImage(for: asset, targetSize: targetSize, contentMode: contentModel, completionHandler: { image in
// Do something with your image if it exists
})
誰かが迅速解決策を探している場合は、ここに拡張したものです:
extension PHAsset {
var thumbnailImage : UIImage {
get {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: self, targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
thumbnail = result!
})
return thumbnail
}
}
}
私はこのエラーを取得する「キャッチされない例外が原因アプリ 『NSInvalidArgumentException』、理由終了:「 - [PHAssetCollection isCloudSharedAssetを]:インスタンスに送信された認識できないセレクタ " – Peter