gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:18:23 28 4
gpt4 key购买 nike

我正在使用 native Swift 制作一个应用程序,用户可以在其中使用 Activity View Controller (AVC) 共享预制图像。这些图像的分辨率非常高,为 1024x1024,但是当这些图像转发到 iOS7 上的“消息”时,顶部和底部会被截断。当图像为 128x128 或在 iOS8 上共享任何分辨率时,确实会发生这种情况。

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

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 "";
}

最佳答案

我已经通过使用多个 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 "";
}

}

获取共享图像:

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");
}
}

关于ios - 如何在 Activity View Controller 中为每个平台共享不同的图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26458021/

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