EXC_BAD_ACCESS使用UIDocumentInteractionController将图像发送到Instagram

时间:2015-01-30 18:41:27

标签: ios swift ios8 uidocumentinteraction

我使用此代码向Instagram发送和图像(以及最终标题):

@IBAction func sendToInstragramPressed(sender: AnyObject) {

  var image = imagePreview.image
  var imagePath = NSString(format: "%@", [NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)][0])

  var img: String = imagePath
  var clean: String = img.stringByReplacingOccurrencesOfString(
  "[\\(\\ \\\"\\)]",
  withString: "",
  options: .RegularExpressionSearch)
  clean = clean.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
  clean = clean + "/image.igo"
  imagePath = clean

  println("test2 \(imagePath)")

  NSFileManager.defaultManager().removeItemAtPath(imagePath, error: nil)
  UIImagePNGRepresentation(image).writeToFile(imagePath, atomically: true)

  var docController: UIDocumentInteractionController = UIDocumentInteractionController(URL: NSURL.fileURLWithPath(imagePath)!)
  docController.delegate = self

  /*var annotationDict: NSMutableDictionary = [:]
  annotationDict["Instagram Caption"] = "Instagram Caption"
  docController.annotation = annotationDict*/

  docController.UTI = "com.instagram.exclusivegram"
  docController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)
}

它显示了要触摸的instagram图标。当我触摸它时,我不断在AppDelegate上收到错误EXC_BAD_ACCESS,但我不能在我的生活中找出导致这种情况的原因。我在objective-c中完成了完全相同的实现,没有问题。可能出现什么问题?

编辑

在我使用此代码块操作imagePath之前发生了这种情况:

var img: String = imagePath
var clean: String = img.stringByReplacingOccurrencesOfString(
  "[\\(\\ \\\"\\)]",
  withString: "",
  options: .RegularExpressionSearch)
clean = clean.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
clean = clean + "/image.igo"
imagePath = clean

1 个答案:

答案 0 :(得分:0)

啊哈!所以看起来我在方法中实例化UIDocumentInteractionController,所以它被ARC破坏了。使它成为一个广泛的变量,并且有魅力

所以一定要在类中实例化它,然后在方法

中引用它

即:

var docController: UIDocumentInteractionController!