I have collectionView
where I am displaying some images, initially from setup collectionview method everything is working fine.
@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")
}
Here comes the problem, whenever there is change in array I am reloading the data but collectionView
getting nil
hence collectionView.reloadData()
is not getting called. What could be the reason? Am I missing something?
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)")
}
}
}
This functions is getting failed when i am trying to record the video and trying to pair the video and photo. The operation couldn't be completed. (Cocoa error -1.)
The operation couldn't be completed. (Cocoa error -1.)
this error i am getting hence getting failed.
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)!)
}
})
}
Any suggestions ?
Thanks in Advance !!
Also you can write code as below in your viewDidLoad rather than implementing setupCollectionView() method:
override func viewDidLoad() { super.viewDidLoad() self.collectionView.delegate = self self.collectionView.datasource = self self.collectionView.register(UINib(nibName: "WallPapersCell", bundle: nil), forCellWithReuseIdentifier: "WallPapersCell") }
The question now references two issues. You should keep the scope of your question limited to a single issue.
For the collectionView issue:
It looks like you are referencing the collectionView from outside of the class. Unless it is initialized from nib, or storyboard it will not create the IBOutlet/IBAction connections.
UPDATE:
From the comments we determined that the VC is being used as a Singleton via a static property and that this will not load the nib and connect the outlets. I would advise to move the shared code into another class/service and use that where needed.
For the exportLiveVideo
issue:
The path that you are passing is:
/LiveWallpapers/\\(uniqueImageName)
You need to pass the full path..
Change
self?.exportLivePhoto(cachePath: "/LiveWallpapers/\(uniqueImageName)")
to
self?.exportLivePhoto(cachePath: output + "/LiveWallpapers/\(uniqueImageName)")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.