如何在Activity View Controller中为每个平台分享不同的图片?

时间:2014-10-20 03:09:51

标签: ios swift messages uiactivityviewcontroller

我正在使用原生Swift创建一个应用程序,用户可以使用活动视图控制器(AVC)共享预制图像。这些图像的分辨率非常高,分辨率为1024x1024,但当这些图像被转发到"消息"在iOS7上,顶部和底部被切断。当图像为128x128或在iOS8上共享任何分辨率时,会发生这种情况。

由于我在显示AVC之前将图像作为PNGDataRepresentation提供,我想知道是否有一种方法可以根据用户选择的平台提供不同的图像。如果用户选择消息与高分辨率,如果选择保存或Facebook共享,我基本上指向较低分辨率的资产。

func openShareWidget ()
{

    var activityProvider: ShareActivityProvider = ShareActivityProvider();
    activityProvider.currentType = self.currentType;

    var imageData : NSData = UIImagePNGRepresentation(selectedCell.getSharingImage());
    var activityItems : NSArray = [activityProvider, imageData];

    var activityController : SharingActivityViewController = SharingActivityViewController(activityItems: activityItems, applicationActivities: []);

    presentViewController(activityController, animated: true, completion: nil);
}     

我的ActivityProvider扩展实现如下:

class ShareActivityProvider: UIActivityItemProvider, UIActivityItemSource {

var currentType : ImageType!;

override func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject?
{
    switch activityType
    {
        case UIActivityTypeMail:
            return "" ;
        case UIActivityTypeMessage:
            return "";
        case UIActivityTypePostToFacebook:
            return "Facebook"
        case UIActivityTypePostToTwitter:
            return "Twitter";
        default:
            return "";
    }
}

override func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject
{
    return "";
}

}

1 个答案:

答案 0 :(得分:0)

我已经通过使用多个UIActivityItem提供程序来实现这一点。一个提供消息体的文本,而第二个提供图像本身。

func openShareWidget ()
{

    var textProvider : ShareTextActivityProvider = ShareTextActivityProvider(); // same as ShareActivityProvider from question       
    var imgProvider : ShareImageActivityProvider = ShareImageActivityProvider()

    var activityItems : NSArray = [textProvider, imgProvider];

    var activityController : SharingActivityViewController = SharingActivityViewController(activityItems: activityItems, applicationActivities: []);

    presentViewController(activityController, animated: true, completion: nil);
}

这是ShareImageActivityProvider

class ShareImageActivityProvider: UIActivityItemProvider {

var selectedCell : ImageCell!;

override func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject?
{
    return UIImagePNGRepresentation(selectedCell.getSharingImage(activityType)); 
}

override func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject
{
    return "";
}

}

GetSharingImage:

func getSharingImage(activityType : String) -> UIImage
  {
    switch activityType
        {
        case UIActivityTypeMessage:
            return UIImage(named: imgPathLOW);
        case UIActivityTypePostToFacebook, UIActivityTypeMail, UIActivityTypePostToTwitter:
            return UIImage(named: imgPathHD);
        default:
            return UIImage(named: "Tex_Empty");
    }
}