3

I am working on PHAsset and NSURL on Swift. I have an image url, for example file:///var/mobile/Media/DCIM/100APPLE/IMG_0043.JPG.

How can i make a PHAsset from this NSURL in Swift?

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256

1 Answers1

7

A PHAsset object represents an image or video file that appears in the Photos app, including iCloud Photos content. To display or edit assets, use the PHAsset class to fetch asset objects for the photos or videos you want to work with. An asset object is immutable and contains only the metadata for the photo or video it represents.

Thus you can not make PHAsset object. Here is only one constructor for making object.

let asset   =   PHAsset()

There is no constructor for NSURL :-

This is what we frequently use:-

let imgData = NSData(contentsOfURL: (NSURL(string: "file:///var/mobile/Media/DCIM/100APPLE/IMG_0043.JPG"))!)
let image = UIImage(data: imgData!)
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34
  • i have found a solution here, http://stackoverflow.com/a/28666212/1084174 . so its possible right? – Sazzad Hissain Khan Aug 26 '16 at 19:09
  • @Sazzad Hissain Khan This will take a lot time if user have 1000's of images – zaheer Oct 16 '18 at 06:40
  • @SazzadHissainKhan, it's possible, if user has this photo in his gallery. But if you get url from web, it is a great chance, that this photo will not be in user gallery. Or you have to previously save photo from url to user gallery. And it's totally inefficient. – Denis Markov Apr 09 '19 at 10:55