0

This question is related to an existing one, but I do not understand the differece of execution.

Myproject development environment is XCode 8.3, Swift 3 and Bridging Objc, iOS 9.0 upper.

I want to display activity image in second row.

When UIActivity's category is action in Swift, activity icon is problem.

Case1. Swift CustomActivity, category type is action.

This isn't displaying image.

override class var activityCategory: UIActivityCategory {
    return .action
}

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

Case2. Swift CustomActivity, category type is share.

This is displaying image.

override class var activityCategory: UIActivityCategory {
    return .share
}

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

Case3. Objective-C CustomActivity

In Objc, override instance variable is correct execute.

+ (UIActivityCategory) activityCategory
{
    return UIActivityCategoryAction;
}

- (UIImage *) _activityImage
{
    return [UIImage imageNamed:@"ic_facebook"];
}

Simulator Images by case

I don't understand, why Objective-C instance parameter is correct execute?

In Swift, Is this inavaliable implements?

What's difference??

Krunal
  • 77,632
  • 48
  • 245
  • 261
Mins
  • 11
  • 2

1 Answers1

2

From the documentation for UIActivity activityImage:

The alpha channel of the image is used as a mask to generate the final image that is presented to the user. Any color data in the image itself is ignored. Opaque pixels have a gradient applied to them and this gradient is then laid on top of a standard background. Thus, a completely opaque image would yield a gradient filled rectangle.

What's missing from this documentation is that this only applies when the activityCategory is set to UIActivityCategoryAction (.action). It used to also apply to UIActivityCategoryShared (.shared) until it changed in an earlier version of iOS (I think it changed in iOS 8).

This is why your first set of Swift code doesn't appear to work. You get the gray rectangle because you need an image mask for type .action.

Your Objective-C code is odd. Why are you implementing - (UIImage *) _activityImage instead of - (UIImage *)activityImage ? Why the underscore?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Your answer thank you. ^^ I'm already `- (UIImage *) activityImage.` This result sames Case1. So I referenced this link. [Reference Link](https://stackoverflow.com/questions/20891730/uiactivityviewcontroller-with-custom-uiactivity-displays-color-image-as-gray) I don't understand, what's `- (UIImage *) _activityImage` and `- (UIImage *) activityImage` execution difference... – Mins Oct 12 '17 at 06:52
  • You are not supposed to override `_activityImage`, just `activityImage`. The one with an underscore is a terrible hack that could break in any future update to iOS. You are supposed to get a gray image for `.action`. and a color image for `.shared`. – rmaddy Oct 12 '17 at 15:14