我在collectionView
上显示一些图像,最初是从 setup collectionview 方法开始的,一切正常。
@IBOutlet weak var collectionView: UICollectionView!
func setupCollectionView(){
DispatchQueue.main.async {
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
self.collectionView.register(UINib(nibName: "WallPapersCell", bundle: nil), forCellWithReuseIdentifier: "WallPapersCell")
}
问题来了,每当数组发生变化时,我都会重新加载数据,但是collectionView
得到nil
,因此collectionView.reloadData()
没有得到调用。可能是什么原因?我想念什么吗?
private var imagePathArray = [String](){
didSet {
print("did set")
if let collectionView = self.collectionView {
collectionView.reloadData()
}
}
}
func loadVideoWithVideoURL(_ videoURL: URL) {
print("load video url \(videoURL)")
// displayImageView.livePhoto = nil
let asset = AVURLAsset(url: videoURL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let time = NSValue(time: CMTimeMakeWithSeconds(CMTimeGetSeconds(asset.duration)/2, preferredTimescale: asset.duration.timescale))
generator.generateCGImagesAsynchronously(forTimes: [time]) { [weak self] _, image, _, _, _ in
if let image = image, let data = UIImage(cgImage: image).pngData() {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let uniqueImageName = videoURL.deletingPathExtension().lastPathComponent
print("/\(uniqueImageName).JPG")
let imageURL = urls[0].appendingPathComponent("\(uniqueImageName).jpg")
try? data.write(to: imageURL, options: [.atomic])
print("Load image url \(imageURL)")
let image = imageURL.path
let mov = videoURL.path
print("image path \(image) and mov path\(mov)")
let output = FilePaths.VidToLive.livePath
print(output)
let assetIdentifier = UUID().uuidString
print(output)
if FileManager.default.fileExists(atPath: output + "/LiveWallpapers"){
print("exits")
}else{
self?.createLiveWallpapersDirectoryIfNotExists(output: output)
print("live wallpapers folder doesnot exists in caches directory")
}
if FileManager.default.fileExists(atPath: output + "/LiveWallpapers/\(uniqueImageName).JPG"){
print("exits")
return
}
JPEG(path: image).write(output + "/LiveWallpapers/\(uniqueImageName).JPG",
assetIdentifier: assetIdentifier)
QuickTimeMov(path: mov).write(output + "/LiveWallpapers/\(uniqueImageName).MOV",
assetIdentifier: assetIdentifier)
self?.imagePathArray.append(output + "/LiveWallpapers/\(uniqueImageName).JPG")
self?.videoPathArray.append(output + "/LiveWallpapers/\(uniqueImageName).MOV")
// here it is getting failed
self?.exportLivePhoto(cachePath: "/LiveWallpapers/\(uniqueImageName)")
}
}
}
当我尝试录制视频并将视频和照片配对时,此功能失败。
The operation couldn’t be completed. (Cocoa error -1.)
这个错误我正因此而失败。
func exportLivePhoto (cachePath : String) {
PHPhotoLibrary.shared().performChanges({ () -> Void in
let creationRequest = PHAssetCreationRequest.forAsset()
let options = PHAssetResourceCreationOptions()
creationRequest.addResource(with: PHAssetResourceType.pairedVideo, fileURL: URL(fileURLWithPath: "\(cachePath).MOV"), options: options)
creationRequest.addResource(with: PHAssetResourceType.photo, fileURL: URL(fileURLWithPath:"\(cachePath).JPG"), options: options)
}, completionHandler: { (success, error) -> Void in
if !success {
NSLog("export live error" + (error?.localizedDescription)!)
}
})
}
有什么建议吗?
预先感谢!
答案 0 :(得分:0)
此外,您可以在 viewDidLoad 中编写以下代码,而不是实现setupCollectionView()方法:
new
答案 1 :(得分:0)
该问题现在涉及两个问题。您应该将问题的范围限制为一个问题。
对于collectionView问题:
似乎您是从类外部引用collectionView的。除非它是从笔尖或情节提要中初始化的,否则将不会创建IBOutlet / IBAction连接。
更新:
根据评论,我们确定VC通过静态属性被用作Singleton,并且不会加载笔尖和连接插座。我建议将共享代码移到另一个类/服务中,并在需要时使用它。
对于exportLiveVideo
问题:
您通过的路径是:
/LiveWallpapers/\(uniqueImageName)
您需要传递完整路径。
更改
self?.exportLivePhoto(cachePath: "/LiveWallpapers/\(uniqueImageName)")
到
self?.exportLivePhoto(cachePath: output + "/LiveWallpapers/\(uniqueImageName)")