Ios Swift-如何从相册加载视频并在“收藏”视图中显示它们
我使用PHImageManager从相册中加载图像。 这是我的密码Ios Swift-如何从相册加载视频并在“收藏”视图中显示它们,ios,swift,video,uicollectionview,Ios,Swift,Video,Uicollectionview,我使用PHImageManager从相册中加载图像。 这是我的密码 var photoLibrary = [UIImage]() func grabPhotos(){ let imgManager = PHImageManager.default() let requestOptions = PHImageRequestOptions() requestOptions.isSynchronous = true reques
var photoLibrary = [UIImage]()
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {
if fetchResult.count > 0 {
for i in 0..<fetchResult.count{
imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
self.photoLibrary.append(image!)
})
}
}
else{
showAllertToImportImage()//A function to show alert
}
}
}
var photoLibrary=[UIImage]()
func grabPhotos(){
让imgManager=PHImageManager.default()
让requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode=.highQualityFormat
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:false)]
如果让fetchResult:PHFetchResult=PHAsset.fetchAssets(带:。图像,选项:fetchOptions){
如果fetchResult.count>0{
对于0..中的i,请参见获取视频的答案和视频的持续时间
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions) {
if fetchResult.count > 0 {
for i in 0..<fetchResult.count{
//Used for fetch Image//
imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
let imageOfVideo = image! as UIImage
self.photoLibrary.append(imageOfVideo)
})
//Used for fetch Video//
imgManager.requestAVAsset(forVideo: fetchResult.object(at: i) as PHAsset, options: PHVideoRequestOptions(), resultHandler: {(avAsset, audioMix, info) -> Void in
if let asset = avAsset as? AVURLAsset {
//let videoData = NSData(contentsOf: asset.url)
let duration : CMTime = asset.duration
let durationInSecond = CMTimeGetSeconds(duration)
print(durationInSecond)
}
})
}
}
else{
//showAllertToImportImage()//A function to show alert
}
}
}
func-grabPhotos(){
让imgManager=PHImageManager.default()
让requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode=.highQualityFormat
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:false)]
如果让fetchResult:PHFetchResult=PHAsset.fetchAssets(带:。视频,选项:fetchOptions){
如果fetchResult.count>0{
因为我在0中…在0中无效
如果let asset=avAsset as?avurlast{
//让videoData=NSData(contentsOf:asset.url)
let duration:CMTime=asset.duration
让durationInSecond=CMTimeGetSeconds(持续时间)
打印(持续时间秒)
}
})
}
}
否则{
//showAllertToImportImage()//用于显示警报的函数
}
}
}
请查看获取视频的答案和视频的持续时间
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions) {
if fetchResult.count > 0 {
for i in 0..<fetchResult.count{
//Used for fetch Image//
imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
let imageOfVideo = image! as UIImage
self.photoLibrary.append(imageOfVideo)
})
//Used for fetch Video//
imgManager.requestAVAsset(forVideo: fetchResult.object(at: i) as PHAsset, options: PHVideoRequestOptions(), resultHandler: {(avAsset, audioMix, info) -> Void in
if let asset = avAsset as? AVURLAsset {
//let videoData = NSData(contentsOf: asset.url)
let duration : CMTime = asset.duration
let durationInSecond = CMTimeGetSeconds(duration)
print(durationInSecond)
}
})
}
}
else{
//showAllertToImportImage()//A function to show alert
}
}
}
func-grabPhotos(){
让imgManager=PHImageManager.default()
让requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode=.highQualityFormat
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:false)]
如果让fetchResult:PHFetchResult=PHAsset.fetchAssets(带:。视频,选项:fetchOptions){
如果fetchResult.count>0{
因为我在0中…在0中无效
如果let asset=avAsset as?avurlast{
//让videoData=NSData(contentsOf:asset.url)
let duration:CMTime=asset.duration
让durationInSecond=CMTimeGetSeconds(持续时间)
打印(持续时间秒)
}
})
}
}
否则{
//showAllertToImportImage()//用于显示警报的函数
}
}
}
我在UICollectionView的单元格中使用UIImageVIew来显示图像。我应该用什么来制作视频??有什么想法吗@Amit VermaThis将为您提供图像,您还可以通过视频数据制作图像,如Thumbimage、imgManager.requestImage(for:fetchResult.object(at:i)作为相位集、targetSize:CGSize(宽度:200,高度:200)、contentMode:.aspectFill、选项:requestOptions、resultHandler:{image,self.photoLibrary.append(image!)中出错)您使用self.photoLibrary.append(imageOfVideo)获取图像。像这样,我使用self.videoDurationArray.append(String(durationInSecond))并添加了以下行,让labelView=cell.viewWithTag(2)func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{…}…NSLog显示索引超出范围:(我在UICollectionView的单元格中使用UIImageVIew来显示图像。我应该在视频中使用什么?有什么想法吗?@Amit VermaThis将为您提供图像,您也可以通过视频数据制作图像,如Thumbimage、imgManager.requestImage(for:fetchResult.object(at:I)作为相位集,targetSize:CGSize(宽度:200,高度:200),contentMode:.aspectFill,options:requestOptions,resultHandler:{image,self.photoLibrary.append(image!)中的错误)您使用self.photoLibrary.append(imageOfVideo)获取图像。就像这样,我使用self.videoDurationArray.append(String(durationInSecond))并在func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{…}NSLog表示索引超出范围:(