gpt4 book ai didi

ios - 让 iOS 操作扩展模板工作

转载 作者:行者123 更新时间:2023-11-29 01:56:11 24 4
gpt4 key购买 nike

我正在 iOS 上尝试操作扩展。我创建了一个新项目并加载了 Action 扩展模板。查看模板代码,似乎如果您选择一个图像,它将把它加载到 ActionViewController 的 imageView 中。我运行了 Action 扩展,它按预期加载到共享对话框中,但是当我选择一个图像时,它没有显示在 ActionViewController 的 imageView 中。熟悉 Action Extensions 的人可以让我知道我缺少什么吗?
感谢
礼萨


enter image description here

最佳答案

如果您正在处理 Swift 项目,在 **ActionViewController 中您需要从 URL 检索图像

你需要像这样添加代码

if let strongImageView = weakImageView {
if let imageURL = image as? NSURL{
strongImageView.image = UIImage(data: NSData(contentsOfURL: imageURL)!)
}else{
strongImageView.image = image as? UIImage
}


}

为了澄清,我在下面添加了完整代码(ActionViewController),请仔细阅读代码,

override func viewDidLoad() {
super.viewDidLoad()


// Get the item[s] we're handling from the extension context.

// For example, look for an image and place it into an image view.
// Replace this with something appropriate for the type[s] your extension supports.
var imageFound = false
for item: AnyObject in self.extensionContext!.inputItems {
let inputItem = item as! NSExtensionItem
for provider: AnyObject in inputItem.attachments! {
let itemProvider = provider as! NSItemProvider
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
// This is an image. We'll load it, then place it in our image view.
weak var weakImageView = self.imageView
itemProvider.loadItemForTypeIdentifier(kUTTypeImage as String, options: nil, completionHandler: { (image, error) in
NSOperationQueue.mainQueue().addOperationWithBlock {


if let strongImageView = weakImageView {

if let imageURL = image as? NSURL{

strongImageView.image = UIImage(data:NSData(contentsOfURL: imageURL)!)

}else{

strongImageView.image = image as? UIImage
}

}


}
})

imageFound = true
break
}
}

if (imageFound) {
// We only handle one image, so stop looking for more.
break
}
}
}

关于ios - 让 iOS 操作扩展模板工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30827924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com