-1

I would like to add the text on my .png image which exist in my swift application and REPLACE my old image with this edited image. But when I am trying to remove old image I am getting the error message :

          Error : Error Domain=NSCocoaErrorDomain Code=513 "“Background.png” 
          couldn’t be removed because you don’t have permission to access it." 
        UserInfo={NSFilePath=/var/containers/Bundle/Application/57134C17-50A5-
    4709-9E3B-8013733175BA/SignatureApp.app/Background.png, NSUserStringVariant=(
                Remove
            ), NSUnderlyingError=0x12cd8c150 {Error 
Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Code which I am using to remove my old png file is :

let path = NSBundle.mainBundle().pathForResource("Background", ofType: "png")!

let fileManager = NSFileManager.defaultManager()
do {
    try fileManager.removeItemAtPath(path)
}
catch let error as NSError {
    print("Error: \(error)")
}

Can anybody give an idea how is it possible to rewrite or delete existing .png file in IOS swift aplication?

Andrey
  • 3
  • 3
  • How did you set that image initially? Using Image Assets in Xcode, right? So replace that file in imageAssets with new file. – Santosh Sep 21 '16 at 21:28
  • No it is not in Image Assets, It is in Xcode directly, for that solution I cannot use Image Assets – Andrey Sep 21 '16 at 21:31

1 Answers1

1

I don't believe you can do this. The image is part of your app bundle. You would need to upload a new application.

Instead you could store the image in the documents or cache directory and in your code check if that file exists and load it instead of Background.png.

Patrick Tescher
  • 3,387
  • 1
  • 18
  • 31
  • This is the approach you should take. You can even write the new path your app should use to `NSUserDefaults` if you need to. This will also let you generate multiple images from scratch, in case, you know, your user changes whatever value you're superimposing. – i_am_jorf Sep 21 '16 at 21:35