9

My app support iOS 7+, I use UIActivity image size: 60pt for iPhone, 76pt for iOS for iPad,

When touch more button to re-order the items, it can't show the icon in the list.

Show share activities

enter image description here

CODE as below:

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
    }
}
}
ZYiOS
  • 5,204
  • 3
  • 39
  • 45
  • Its preferable to upload screens with english text. Because everyone can understand english but not this language (I'm guessing its Chinese). – Hemang Dec 17 '14 at 09:31
  • From [here](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivity_Class/index.html#//apple_ref/occ/instm/UIActivity/activityImage). The image is used to generate a button for your service in the UI displayed by the UIActivityViewController object. I think the image is for the icon of custom activity in your first picture. The more button is for rearrange the activities, be it a custom or built in one. Icon is not shown for custom activity.you can use the built in Weibo and TencentWeibo icon as long as you have them installed on your iPhone. – gabbler Dec 22 '14 at 08:32
  • For others like wechat, you can use a custom share sheet just like what the ShareSDK library does, which is shown in your code. – gabbler Dec 22 '14 at 08:35
  • 1
    @gabbler Thanks, ShareSDK is OK, but I want to use iOS system provided UI. – ZYiOS Dec 23 '14 at 06:26
  • 1
    Then, I am afraid there is no way to show that icon. – gabbler Dec 23 '14 at 09:35
  • @gabbler That's too bad – ZYiOS Dec 25 '14 at 01:51
  • It is ok, I don't think it would have any severe consequence. – gabbler Dec 25 '14 at 02:06

4 Answers4

6

Quite old question, but maybe the solution is helpful for others too!

In your UIActivity subclass implement/overwrite the method

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

to return the image to be shown in the UIActivityViewController itself (as you've done).

In addition to this implement/overwrite the method

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

to return an(other or the same) image to be shown in the more/settings view.

I haven't found the second method in the docs, but it is none of the '_xx' method of UIAction, so I would guess it isn't private...

LaborEtArs
  • 1,938
  • 23
  • 27
3

UIActivity has an undocumented activitySettingsImage property. The following code shows how to implement it with Swift 3 in order to display a thumbnail of your custom activity in the "Activities" list:

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
    }

    /* ... */
}

Note that you don't have not override activitySettingsImage to implement it in your code.

Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
3

I found the solution in Swift.

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

The image is 29x29 + @2x + @3x. Rather activityImage, no mask is apply to the image. If you want rounded corners, you must add them.

Works with Swift 5 on iOS 12

0

I have encountered the same problem in my project in Swift. I tried to use @imanou-petit answer, but had no success fixing icon bug. But, then I tried to write my UIActivity subclass in objective-c(despite my project is in Swift), with activitySettingsImage method, like @laboretars wrote, and it worked! That's strange, but it worked.

locomotion
  • 1,568
  • 3
  • 11
  • 16