简体   繁体   English

iOS Swift App Extension共享图像

[英]ios swift app extension to share image

I'm trying to follow 2.2 from Vandad Nahavandipoor's book iOS 8 Swift Programming Cookbook O'REILLY Nov, 2014 first edition. 我正在尝试遵循Vandad Nahavandipoor的书iOS 8 Swift Programming Cookbook O'REILLY,2014年11月的第一版2.2。

I understand and have corrected some bugs in the text already, but this one has me stumped. 我了解并已更正了文本中的一些错误,但是这一点让我感到困惑。

I'm trying to create a share extension for my app so that people can upload images to my site. 我正在尝试为我的应用创建共享扩展名,以便人们可以将图像上传到我的网站。

The extension works in that it appears in the last and when you click it, it gives you a place to put the title and shows the image, however, the POST is not active but disabled. 该扩展名的工作方式是出现在最后,单击该扩展名后,可以在其中放置标题并显示图像,但是POST未激活但已禁用。 I'm new to iOS so I am not sure quite what to do. 我是iOS的新手,所以我不确定该怎么做。

In the section he goes on to add an audience selector which I skipped, I have a feeling that might be part of it, but I don't have that option so I didn't put it in. 在本节中,他继续添加了一个我跳过的受众选择器,我觉得可能是其中的一部分,但是我没有该选项,所以没有放入。

Here is the code for my share extension 这是我的股票扩展程序的代码

import UIKit
import Social
import MobileCoreServices


class ShareViewController: SLComposeServiceViewController, NSURLSessionDelegate {

var imageData: NSData?

override func isContentValid() -> Bool {    
    if let data = imageData {
        if countElements(contentText) > 0 {
            return true
        }
    }
    return false
}

override func presentationAnimationDidFinish() {
    super.presentationAnimationDidFinish()
    placeholder = "Title"

    let content = extensionContext!.inputItems[0] as NSExtensionItem
    let contentType = kUTTypeImage as NSString

    for attachment in content.attachments as [NSItemProvider]{
        if attachment.hasItemConformingToTypeIdentifier(contentType){
            let dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
            dispatch_async(dispatchQueue, {[weak self] in
                let strongSelf = self!
                attachment.loadItemForTypeIdentifier(contentType, options: nil, completionHandler: {(content: NSSecureCoding!, error: NSError!) in
                    if let data = content as? NSData {
                        dispatch_async(dispatch_get_main_queue(), {
                            strongSelf.imageData = data
                            strongSelf.validateContent()
                        })
                    }
                })
            })
        }
        break
    }
}

override func didSelectPost() {
    //TODO if no FBSession - then don't do it
    let token = FBSession.activeSession().accessTokenData.accessToken

    let identifier = NSBundle.mainBundle().bundleIdentifier! + "." + NSUUID().UUIDString
    let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)

    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    let url = NSURL(string: "http://...&text="+self.contentText)

    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.HTTPBody = imageData!

    let task = session.uploadTaskWithRequest(request, fromData: request.HTTPBody)

    task.resume()

    extensionContext!.completeRequestReturningItems([], completionHandler: nil)

}

// override func configurationItems() -> [AnyObject]! {
    // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
//    return NSArray()
//}

}

I just want to enable the POST action in the share popup. 我只想在共享弹出窗口中启用POST操作。 thanks in advance 提前致谢

EDIT

continuing on, I've discovered the issue is that the imageData of the selected image is nil. 继续,我发现问题是所选图像的imageData为nil。 content holds the value 内容拥有价值

file:///var/mobile/Media/DCIM/100APPLE/IMG_0001.JPG

at the line 在行

if let data = content as? NSData{ ...}

fails. 失败。 how do I read in this image as NSData? 我如何在此图像中读取为NSData?

The solution is 解决方案是

image = UIImage(data: NSData(contentsOfURL: content as NSURL)!)

thanks to 谢谢

Unable to cast UIImage in swift iOS 8 Extension 无法在快速的iOS 8 Extension中投射UIImage

您将要使用NSData.dataWithContentsOfFile(content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM