UIActivity自定义共享,触摸更多按钮时不显示缩略图图标

时间:2014-12-13 04:13:58

标签: ios iphone share uiactivity uiactivitycategory

我的应用程序支持iOS 7+,我使用UIActivity图像大小:iPhone为60pt,iOS为iPad为76pt,

当触摸更多按钮重新排序项目时,它无法在列表中显示该图标。

Show share activities

enter image description here

代码如下:

class ZYShare {
class ActivityItem {
    init() {
        self.title = ""
        self.icon = ""
    }

    var title: String?
    var icon: String?
    var type: ShareType = ShareTypeAny
}

class CommonActivity: UIActivity {
    lazy var itemInfo = ActivityItem()

    override func prepareWithActivityItems(activityItems: [AnyObject]) {
        // do something

    }

    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        return true
    }

    override class func activityCategory() -> UIActivityCategory {
        return UIActivityCategory.Action
    }

    override func activityType() -> String? {

        return NSLocalizedString(itemInfo.title ?? "", comment: "")
    }

    override func activityTitle() -> String? {
        return NSLocalizedString(itemInfo.title ?? "", comment: "")
    }

    override func activityImage() -> UIImage? {
        println(itemInfo.icon)

        if let icon = itemInfo.icon {
            return UIImage(named: icon)
        } else {
            return nil
        }
    }

    override func performActivity() {
        ShareSDK.showShareViewWithType(itemInfo.type
            , container: nil
            , content: publishContent
            , statusBarTips: false
            , authOptions: authOptions
            , shareOptions: options
            , result: handle)

        self.activityDidFinish(true)
    }
}

class QQActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToQQ"
        self.itemInfo.title = "QQ"
        self.itemInfo.type = ShareTypeQQ
    }
}

class WeChatSessionActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToWeChat"
        self.itemInfo.title = "微信"
        self.itemInfo.type = ShareTypeWeixiSession
    }
}

class WeChatTimelineActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToWeChatTimeLine"
        self.itemInfo.title = "朋友圈"
        self.itemInfo.type = ShareTypeWeixiTimeline
    }
}

class QQSpaceActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToQzone"
        self.itemInfo.title = "QQ空间"
        self.itemInfo.type = ShareTypeQQSpace
    }
}
}

3 个答案:

答案 0 :(得分:5)

相当古老的问题,但也许解决方案对其他人也有帮助!

UIActivity 子类中实现/覆盖方法

- (UIImage *)activityImage {
    return [UIImage imageNamed:@"Activity Icon"];
}

返回要在UIActivityViewController中显示的图像(正如您所做的那样)。

除了这个工具/覆盖方法

- (UIImage *)activitySettingsImage {
    return [UIImage imageNamed:@"Activity Settings Icon"];
}

返回更多/设置视图中显示的(其他或相同)图像。

我还没有在文档中找到第二种方法,但它并不是' _xx' UIAction的方法,所以我猜它不是私人......

答案 1 :(得分:1)

UIActivity有一个未记录的activitySettingsImage属性。以下代码显示了如何使用Swift 3实现它,以便在“活动”列表中显示自定义活动的缩略图:

import UIKit

class MyActivity: UIActivity {

    override var activityType: UIActivityType? {
        return UIActivityType(rawValue: String(describing: classForCoder))
    }

    override var activityTitle: String? {
        return "My Activity Title"
    }

    override var activityImage: UIImage? {
        return UIImage(named: "icon")
    }

    var activitySettingsImage: UIImage? {
        return UIImage(named: "icon")
    }

    override class var activityCategory: UIActivityCategory {
        return .share
    }

    /* ... */
}

请注意,您没有覆盖activitySettingsImage以在代码中实现它。

答案 2 :(得分:0)

我在Swift中找到了解决方案。

@objc var _activitySettingsImage: UIImage? {
    return UIImage(named: "icon")!
}

图像为29x29 + @ 2x + @ 3x。 而不是activityImage,没有遮罩应用于图像。如果要圆角,必须添加它们。

在iOS 12上与Swift 5配合使用