I am trying to use the PHPickerViewController to select images from my photo gallery and seeing the following error.
Could not create a bookmark: NSError: Cocoa 257 "The file couldn’t be
opened because you don’t have permission to view it
I am using Xcode 12.5 on Mac M1 (macOS 11.2.3) And connecting to an iPhone 12 (IOS 14.8).
The code in question appears to throw the above exception in the call to loadFileRepresentation. I have also tried loadInPlaceFileRepresentation and loadObject with similar results.
private func getPhoto(from itemProvider: NSItemProvider) {
print(itemProvider.registeredTypeIdentifiers)
itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) { url, error in
if let error = error {
print("Encountered Error in loadFileRepresentation \(error.localizedDescription)")
}
if url != nil {
print("Obtained url: \(String(describing: url))")
} else {
print("Could not obtain url")
}
let imageData = try! Data(contentsOf: url!)
DispatchQueue.main.async {
self.parent.mediaItems.append(item: PhotoPickerItem(with: UIImage(data:imageData)!))
}
}
}
I can see that the itemProvider has the following representations.
["public.heic", "public.jpeg"]
and I have tried UTType.jpeg.identifier as well.
Any pointers would be greatly appreciated
Related
While trying to save a photo to a custom album on some devices we get the following error:
The operation couldn’t be completed. (PHPhotosErrorDomain error 3300).
code: 3300
This is the part of the code where the issue happens
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
guard let placeholder = assetChangeRequest.placeholderForCreatedAsset else { return }
let albumChangeRequest = PHAssetCollectionChangeRequest(for: album)
albumChangeRequest?.addAssets([placeholder] as NSArray)
}, completionHandler: { success, error in
completion(success, error)
})
I would be thankful for any tips since I am out of ideas.
#available(iOS 8, *)
open func addAssets(_ assets: NSFastEnumeration)
have a try albumChangeRequest.addAssets([placeholder] as NSFastEnumeration)
here is the Apple Document
I'm creating an app with App Extension and I'm having trouble uploading files to firebase storage.
I choose a photo that I chose from the Photos application or another application for my own application by pressing the share button.
I am getting the URL of the selected photo in ShareViewController.
if let items = (self.extensionContext?.inputItems.first as? NSExtensionItem)?.attachments {
let contentType = kUTTypeData as String
for item in items {
item.loadItem(forTypeIdentifier: contentType, options: nil) { url, error in
if let path = url as? NSURL {
URLs.append(path.absoluteString!)
}
else {}
}
}
}
I'm trying to upload the URLs I get this way to firebase storage.
let uploadTask = riversRef.putFile(from: URLs[0], metadata: nil) { metadata, error in
guard let metadata = metadata else {
return
}
...
}
There is a problem with the URL of the selected photo but I can't quite understand it. I can get the size, name and other properties of the photo from the URL. I can even copy this photo to another directory with FileManager. I'm getting the URL from FileManager but still getting the same error. But when I try to install I get the following error.
Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response." UserInfo={bucket=appBucketName, _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundUploadTask <9381D3C6-241C-4737-9589-BA5A7CFAF9E4>.<1>, object=PATH/6B36F026-CD6E-4E00-8A19-C530DC606674.jpg, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"BackgroundUploadTask <9381D3C6-241C-4737-9589-BA5A7CFAF9E4>.<1>"
), NSLocalizedDescription=An unknown error occurred, please check the server response., ResponseErrorDomain=NSURLErrorDomain, ResponseErrorCode=-995}
Solutions I tried;
I will not use putData.
I tried App Groups. I already have the URL of the file. I need to install directly but it doesn't work.
extension FileManager {
func documentsDirectory() -> URL {
let path = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.name")
return path!
}
func uploadPath(srcURL: URL, name: String, completion: #escaping ((_ filePath: URL?) -> Void)) {
do {
let path = documentsDirectory().appendingPathComponent("tempory")
try FileManager.default.createDirectory(atPath: path.relativePath, withIntermediateDirectories: true, attributes: nil)
let fullPath = path.appendingPathComponent(name)
try FileManager.default.copyItem(at: srcURL, to: fullPath)
completion(fullPath)
}
catch {
completion(nil)
}
}
}
Note: I am using Firebase emulator and Xcode simulator. I don't have any problems with the main application.
If there is missing information or incorrect information, please warn, I will correct it. Thank you for your help in advance.
I am facing a weird problem with an .m4a sound file on an iOS app written in Swift.
Here is what happens:
I record a sound file on the device using the app. Let us call it xyz.m4a.
I check that I can play this sound file, and it is working.
I then upload the sound file to an AWS S3 bucket, from my app.
I check on the AWS console that the file is uploaded as expected and I download it using a browser (on my computer), checking again that I can still play the sound as expected. Up to this point nothing is wrong, I can play the sound.
I then use my app to download the file to my iOS device (iPhone 6). All seems OK here too.
Finally I try to play the downloaded file from within my app and this is where things go wrong. I cannot play it.
Here is what I think is the relevant code, please let me know if you think I should add more.
Code to download the file from the AWS S3 bucket:
let transferUtility = AWSS3TransferUtility.default()
transferUtility.downloadData(
fromBucket: "mybucket",
key: "xyz.m4a",
expression: expression,
completionHandler: completionHandler
).continueWith {
(task) -> AnyObject! in if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with downloadTask.
print("task.result -- OK!")
let downloadOutput = task.result
print("downloadOutput:\(String(describing: downloadOutput))")
}
return nil;
}
Here is the completionHandler part:
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
completionHandler = {
[weak self] (task, URL, data, error) -> Void in
DispatchQueue.main.async(execute: {
print("transfer completion OK!")
let localFileName = "xyz.m4a",
playFileURL = self?.getDocumentsURL().appendingPathComponent(localFileName)
FileManager.default.createFile(atPath: (playFileURL?.path)!,
contents: data,
attributes: nil)
if FileManager.default.fileExists(atPath: (playFileURL?.path)!) {
print("playFileURL present!") // Confirm that the file is here!
}
})
}
Code to play the downloaded sound file:
let fileName = "xyz.m4a",
audioFile = getDocumentsURL().appendingPathComponent(fileName)
if !FileManager.default.fileExists(atPath: audioFile.path) {
print("We have no file !!!!!") // Just in case the file was not found!
}
// This code assumes the file exists.
do {try soundSession.setCategory(AVAudioSessionCategoryPlayback)
try soundSession.setActive(true)
if audioPlayer == nil {
audioPlayer = try AVAudioPlayer(contentsOf: audioFile,
fileTypeHint: AVFileType.m4a.rawValue)
guard let _ = audioPlayer else {return}
audioPlayer.delegate = self
}
audioPlayer.play()
} catch let error {
print("Error:\n" + error.localizedDescription)
}
Finally here is the error I get:
Error:
The operation couldn’t be completed. (OSStatus error 1685348671.)
I hope someone can see something wrong an tell me where the problem is.
Guys i was making a simple application using Firebase and i was uploading an image and also downloading it sometime later. Uploading works perfectly well but when i try to download an error occurs that says. Object profile_pics not found. I have created a folder named profile_pics and i store the image inside that folder. The image is there but gives error while downloading. Here is the screen shot.
Here is what is in the console.
Error -: Object profile_pics does not exist.
Here is the code to download the image.
let storage = FIRStorage.storage().reference(forURL: "gs://dd-dd-dd.appspot.com/profile_pics/\(profileURL)")
print("Path Full \(storage.fullPath)")
storage.data(withMaxSize: 11 * 1024 * 1024, completion: {
(data, error) in
if let error = error{
print("Error -: \(error.localizedDescription)")
}
else{
let image = UIImage(data: data!)
cell.profileImageView.image = image
}
})
What could be the possible error? If you please guide me through this.
I recover it this way.
let url = URL(string: "\(ImageUrlSavedinDatabase)")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.sync() {
self.profilepicimageview.image = UIImage(data: data)
self.actIndicator.stopAnimating()
}
}
task.resume()
Does that work?
Niall
I am using swift PHPhotoLibrary class to save image in photo library. Below is my code:
PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
let assetCreation = PHAssetCreationRequest.creationRequestForAssetFromImage(self.photoView.image!)
let imageData = UIImageJPEGRepresentation((self.photoView?.image)!,1)
let option = PHAssetResourceCreationOptions()
assetCreation.addResourceWithType(PHAssetResourceType.FullSizePhoto, data: imageData!, options: option)
},
completionHandler: {
success, error in
if success {
print("save image success" )
} else {
print(error)
}
})
But I always got this error message: Optional(Error Domain=NSCocoaErrorDomain Code=-1 "(null)")
This error was printed by the else line: "print(error)"
Does anyone know what the problem is?