Apple IOS8 Action Extension模板不适用于Photo

时间:2014-09-30 09:24:49

标签: swift ios8

我试图获取在向项目添加动作扩展时自动创建的动作扩展模板。默认情况下,它假设接受图像并显示它。但是当试图在Photo中使用它时,图片是jpeg和imageView.image =图像为? UIImage没有设置图像。 imageView.image是零。

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 NSString) {
            // This is an image. We'll load it, then place it in our image view.
            weak var weakImageView = self.imageView
            itemProvider.loadItemForTypeIdentifier(kUTTypeImage as NSString, options: nil, completionHandler: { (image, error) in
                if image != nil {
                    NSOperationQueue.mainQueue().addOperationWithBlock {
                        if let imageView = weakImageView {
                            imageView.image = image as? UIImage
                        }
                    }
                }
            })

            imageFound = true
            break
        }
    }

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

但Objective C等效工作正常

BOOL imageFound = NO;
for (NSExtensionItem *item in self.extensionContext.inputItems) {
    for (NSItemProvider *itemProvider in item.attachments) {
        if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
            // This is an image. We'll load it, then place it in our image view.
            __weak UIImageView *imageView = self.imageView;
            [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
                if(image) {
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        [imageView setImage:image];
                    }];
                }
            }];

            imageFound = YES;
            break;
        }
    }

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

是否有人能够使模板正常工作?

您可以转到“文件/新/目标”获取此代码。从Xcode 6.0.1中的Swift项目中选择“应用程序扩展/操作扩展”

1 个答案:

答案 0 :(得分:0)

我现在有一个解决方法,可能对其他人发现同样的问题很有用:

我创建了一个目标c实用程序类,它从目标c调用“loadItemForTypeIdentifier”并返回图像。我从Swift调用这个函数并且运行得很好。

@protocol UtilitiesDelegate
- (void) setImage:(UIImage*) image;
@end

@implementation Utilities
+ (void) loadItemForImage:(NSItemProvider *)itemProvider delegate:(id<UtilitiesDelegate>)utilDelegate
{
    [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
        if(image)
        {
            [utilDelegate setImage:image];
        }

    }];

}

你可以致电

Utilities.loadItemForImage(itemProvider, delegate: self)

而不是

itemProvider.loadItemForTypeIdentifier(kUTTypeImage as NSString, options: nil, completionHandler: { (image, error) in
        if image != nil {
            NSOperationQueue.mainQueue().addOperationWithBlock {
                if let imageView = weakImageView {
                    imageView.image = image as? UIImage
                }
            }
        }

我知道这不是一个正确的解决办法,但它确实有效。