ios - EXC_BAD_ACCESS 使用 UIDocumentInteractionController 将图像发送到 Instagram

标签 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

最佳答案

啊哈!所以看起来好像我在方法中实例化了 UIDocumentInteractionController,所以它被 ARC 丢弃了。使它成为类范围内的变量,并发挥了作用

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

即:

var docController: UIDocumentInteractionController!

关于ios - EXC_BAD_ACCESS 使用 UIDocumentInteractionController 将图像发送到 Instagram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242583/

相关文章:

ios - 在 iOS 上通过 URL 方案启动 Viber 应用程序

ios - 白色透明工具栏,如何使状态栏匹配

swift - NSAttributedText 不适用于 Swift 中的 UILabel

ios8 - AVPlayerItem videoComposition 卡住IOS8

ios - 无法从设备中删除 currentUser.uid

ios - 如何减少小数位数?

swift - UIViewRepresentable 及其内容的固有大小

ios - 错误 - EXC_BREAKPOINT(代码=1,子代码=0x100308448)

arrays - 是否可以在 Swift 中创建一个仅限于一个类的数组扩展?

xcode - Interface Builder 无法确定 Storyboard的类型