При обмене изображениями, а точнее большим gif, я получаю ошибку
photos Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"
Что это значит, и как я могу это исправить?
Вот сокращенная версия функции сохранения моего кода, оставляя только части, связанные с GIF-файлами.
@IBAction func saveAction(_ sender: Any) {
....
else{
//Save the image
prefs.set(currentPics, forKey: "numPics")
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
print(content.description)
let contentType = kUTTypeImage as String
let gifType = kUTTypeGIF as String
if let contents = content.attachments {
for attachment in contents {
var isGif=false
//Check for gif
if attachment.hasItemConformingToTypeIdentifier(gifType) {
isGif=true
attachment.loadItem(forTypeIdentifier: contentType, options: nil) { data, error in
let url = data as! URL
if let imageData = try? Data(contentsOf: url) {
self.saveImage(imageData: imageData, isGif: isGif, fileURL: url)
}
}
}
//Not a gif
...........
}
}
}
func saveImage(imageData: Data, isGif: Bool, fileURL: URL) {
let ArchiveURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier:suiteName)!
do{
let directoryContents = try FileManager.default.contentsOfDirectory( at: ArchiveURL, includingPropertiesForKeys: nil, options: [])
let pngFiles = directoryContents.filter{ $0.pathExtension == "png" }
let gifFiles = directoryContents.filter{ $0.pathExtension == "gif" }
let number = pngFiles.count+gifFiles.count
.......
else{
//Insert code to handle resizing here
print("HERE WE GO")
let parameters = [
"version": "1",
"mode": "resize"
]
do{
print("contacting server")
}
try imageData.write(to: ArchiveURL.appendingPathComponent("tmp.gif"), options: Data.WritingOptions.atomic)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(fileURL, withName: "gif")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to: "https://my.heroku.app.link",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.validate()
upload.responseData { response in
let responseValue = response.result.value
let dispatchWorkItem = DispatchWorkItem{
print("work item start")
print("work item end")
}
self.delay(bySeconds: 10, dispatchLevel: .main, closure: dispatchWorkItem)
do {
//Check for errors
var dataString = String(data: response.data!, encoding: String.Encoding.utf8)
print(dataString)
if dataString == "error"{
do{
print("error")
}
}
else{
//If it's not, save it
print("Writing")
print(responseValue?.description)
print(responseValue!.isImageData)
if responseValue!.isImageData{
try responseValue!.write(to: filename, options: Data.WritingOptions.atomic)
if let prefs = UserDefaults(suiteName: self.suiteName) {
var currentPics=prefs.integer(forKey: "numPicks")
print(currentpics)
currentPics+=1
prefs.set(currentPics, forKey: "numPics")
print(currentPics)
}
do{
print("Saved")
}
}
..........
}
Странно то, что иногда это просто работает. Но в большинстве случаев это будет запрещено.