3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】PhotoLibraryからPeopleアルバムを取得したかった話

Last updated at Posted at 2021-06-03

結論

現状、取得できない。

stack overflowで見つけた記事によると、"Peopleアルバムにアクセスできないのはバグor意図的に隠されてる可能性がある"とのこと。

取得できそうに見えるけどできない

通常、fetchAssetCollections(with:subtype:options:)でPhotoLibraryから取得したいアルバムを指定する。
with(内部引数はtype)には、ユーザが作成したアルバムか、システムが作成したアルバムかを指定。subtypeに具体的なアルバムの指定をする。

let collections = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumSyncedFaces, options: nil)
collections.enumerateObjects { collection, _, _ in
      // 表示可能なアルバム名をprint
      print(collection.localizedTitle!)
}

お...?subtype.albumSyncedFacesとやらを指定できるっぽい...Peopleって顔認識だろうしこれっぽい...いける!!!

表示可能なアルバム名
Panoramas
Videos
Favorites
Time-lapse
Hidden
Recently Deleted
Recents
Bursts
Slo-mo
Recently Added
Selfies
Screenshots
Portrait
Live Photos
Animated
Long Exposure
Unable to Upload

Peopleがない...
調べてみると.albumSyncedFacesというのは、Peopleに似た古い機能(アルバム)を指しているっぽい。
昔、iPhotoという、iTunesを介してiOSデバイスに同期する機能?があり、その中で、顔認識を使用して写っている人物をグループ化してアルバムにしていたらしい。

ドキュメントにも似たようなことが書かれてた

A Faces group synced to the device from iPhoto.
https://developer.apple.com/documentation/photokit/phassetcollectionsubtype/albumsyncedfaces

まとめ

元々は「iPhoneに保存されている写真から特定の人物の写真だけを抽出して〜...」と色々とやってみたいことがあり、Create MLで特定人物の顔を学習させたModelを生成すればいいのかなと思ったが、精度をあげるには色々と調整したり面倒なので、いっそのことPeopleアルバムから取得すればいいじゃん!って思ったが、そううまいこといかないようで残念。
まあ、悪意のある開発者が、第三者の端末の写真から特定人物の写真だけ裏で取得して...みたいなことが簡単にできるとセキュリティ的によくないだろうし使えないままで正解なのかもしれない。

参考

PHAssetCollectionType早見表
iOS Photos Framework: is it possible to access photos from “People” smart folder?
Access “People ” album and getting images from it [duplicate]

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?