Help us understand the problem. What is going on with this article?

Photo Stream と PHAsset.fetchAssetsWithALAssetURLs の甘く切ない関係

More than 3 years have passed since last update.

内容的には、下のリンクと同じ内容です。ちょっと違うやり方をしたというだけ。なぜPHAsset.fetchAssetsWithALAssetURLsがPhotoStreamの場合だけきちんと値を返却してくれないのかと。。
- http://qiita.com/onizine/items/69b6cd4292add3cbadc0
- http://stackoverflow.com/questions/26588496/loading-image-from-my-photo-stream-using-uiimagepicker-results-url-and-phasset

単にfor文で回したくない :weary: というだけです。

    class func fetchAssetWithALAssetURL (alURL: NSURL) -> PHAsset? {
        var phAsset : PHAsset?

        let optionsForFetch = PHFetchOptions()
        optionsForFetch.includeHiddenAssets = true

        let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([alURL], options: optionsForFetch)
        if fetchResult.count > 0 {
            return fetchResult[0] as? PHAsset
        }
        let str = alURL.absoluteString
        let localIDFragment = str.componentsSeparatedByString("=")[1].componentsSeparatedByString("&")[0]

        let fetchResultForPhotostream = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.AlbumMyPhotoStream, options: nil)
        if fetchResultForPhotostream.count > 0 {
            let photostream = fetchResultForPhotostream[0] as! PHAssetCollection
            let fetchResultForPhotostreamAssets = PHAsset.fetchAssetsInAssetCollection(photostream, options: optionsForFetch)
            if fetchResultForPhotostreamAssets.count >= 0 {
                let i=NSIndexSet(indexesInRange: NSRange(location: 0,length: fetchResultForPhotostreamAssets.count))
                // 逆順で列挙する。フォトストリームが後から表示されているのでその方が効率的のはず
                fetchResultForPhotostreamAssets.enumerateObjectsAtIndexes(i,
                    options: NSEnumerationOptions.Reverse,
                    usingBlock: {
                        (obj, idx, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
                        let target = obj as! PHAsset
                        let ident = target.localIdentifier
                        if let _ = ident.rangeOfString(localIDFragment) {
                            LOG("found!")
                            phAsset = target
                            stop.memory = true
                        }
                })
                return phAsset
            }
        }
        return nil
    }

enumrateObjectsAtIndexesを利用する際の覚書です。
ポイントとしては、パラメータで渡す NSIndexSet の設定と、見つかった際に途中終了する方法でしょうか。stopをどうすれば良いのかというところがわかりにくかった。

to_obara
https://twitter.com/to_obara 知らないことだらけ
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした