I want to take local assets(videos,photos) shared to Facebook,but Facebook's API require I must offer a url in local system gallery,How can I get it? thanks.
Asked
Active
Viewed 254 times
1
-
what approach are you using to pick photo or videos ? `UIImagePickerController` ? – Ketan Parmar Sep 29 '16 at 09:50
-
no. I use Photos Foundation get gallery all asset,and I can get every asset local Identifier. – Littlecoder Sep 29 '16 at 09:56
-
how can I though offering a asset to obtain the asset's url? – Littlecoder Sep 29 '16 at 09:58
-
- (PHFetchResult *)getAssetsInCustomeAssetCollection this method can get all assets – Littlecoder Sep 29 '16 at 09:59
-
show your code in question that how you doing this! – Ketan Parmar Sep 29 '16 at 09:59
-
typedef void(^Result)(NSData *data,NSURL *url) + (void)getDataFromPHAsset:(PHAsset *)asset Complete:(Result)result. – Littlecoder Sep 29 '16 at 10:02
-
this is I expected – Littlecoder Sep 29 '16 at 10:03
-
Try this http://stackoverflow.com/questions/26025487/nsurl-from-phasset – Sanjeet Verma Sep 29 '16 at 10:47
1 Answers
0
first Didload() code like this
self.options.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.videoHighFrameRate.rawValue)
self.images = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: nil)
self.options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
self.collectionView.reloadData()
self.collectionView.dataSource = self
self.collectionView.delegate = self
And in didselect() code like below
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath as IndexPath) as! custCollectionViewCell
var videoURL: NSURL!
var resourceArray: [AnyObject] = PHAssetResource.assetResources(for: self.images[indexPath.row] )
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as [AnyObject]
self.assetResource = resourceArray[0] as! PHAssetResource
let filePath = paths[0].appending("/image.mov")
videoURL = NSURL.init(fileURLWithPath: filePath )
do
{
try FileManager.default.removeItem(atPath: filePath )
}
catch
{
}
PHAssetResourceManager.default().writeData(for: self.assetResource, toFile: videoURL as URL, options: nil, completionHandler: {_ in
})
let assets = AVAsset(url: NSURL(fileURLWithPath: filePath) as URL)
let length = Float(assets.duration.value) / Float(assets.duration.timescale)
if length != 0.0 {
self.selectedImage = cell.photoImageView.image
performSegue(withIdentifier: "show", sender: self)
}
}
Still you need any help feel free to ask me

JAck
- 854
- 9
- 18