我正在尝试对标准活动(Print,Mail,FaceBook等)实施自定义活动,但现在只需要标准打印(用于AirPrint)和我自己的直接方法自定义打印。我显然缺少一些基本的东西,因为我的自定义类中的方法都没有被调用。现在我只有一些NSLog语句来确定调用序列,并让框架运行。
以下是我的自定义活动类的测试代码:
// PrintActivity.h
#import <UIKit/UIKit.h>
@interface PrintActivity : UIActivity
@end
而.m
#import "PrintActivity.h"
@interface PrintActivity ()
@property (nonatomic, strong) UIWebView *dummyWebView;
@end
@implementation PrintActivity
- (NSString *)activityType {
NSLog(@"activityType");
return @"MetriScan Print";
}
- (NSString *)activityTitle {
NSLog(@"activityTitle");
return @"MetriScan\nPrint";
}
- (UIImage *)activityImage {
NSLog(@"activityImage");
UIImage *icon = [UIImage imageNamed:@"metriscan_57_c2a_3.png"];
return icon;
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
NSLog(@"canPerformWithActivityItems");
return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems {
NSLog(@"prepareWithActivityItems");
}
- (void)performActivity {
NSLog(@"Do the actual printing here");
// My custom code here
}
这是主程序中的调用:
- (IBAction)printReport:(UIBarButtonItem *)sender {
NSLog(@"Print Report");
PrintActivity *metriscanPrint = [[PrintActivity alloc] init];
UIViewPrintFormatter *printFormatter = [self.webView viewPrintFormatter];
NSArray *activityItems = [NSArray arrayWithObjects:printFormatter, nil];
NSArray *appActivities = [NSArray arrayWithObjects:metriscanPrint, nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:appActivities];
//activityController.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypeMail, UIActivityTypeMessage, nil];
activityController.completionHandler = ^(NSString *activityType, BOOL completed) {
sender.enabled = YES;
};
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self presentViewController:activityController animated:YES completion:nil];
} else {
sender.enabled = NO;
self.printPop = [[UIPopoverController alloc] initWithContentViewController:activityController];
[self.printPop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
正如我所说,自定义类中的方法都没有被调用,但系统的邮件,消息和复制图标显示在活动表中,而不是打印图标。我只期望系统打印图标(和我自己的)。
如果我进一步取消注释顶部语句块(并注释掉NSArray * activityItems ...........),则系统会显示邮件,消息,打印和复制图标。在这个实验中,我认为我通过创建自己的格式化程序来混合不同的方法,但这似乎是WWDC 2012的建议?
如果我然后使用&#39; excludeActivityTypes&#39;取消注释该行。我只获得系统打印图标。
我欢迎任何意见,以帮助我解决这个问题。
如果有人知道任何示例代码可以做我想要的,那就太棒了。
修改:更新了我的工作代码的代码。
答案 0 :(得分:7)
我过去的时候也把头发拉过UIActivity,苹果确实需要更好地解释它并添加更多功能;试试这个:
PrintActivity.h
#import <UIKit/UIKit.h>
@interface PrintActivity : UIActivity
@end
PrintActivity.m
#import "PrintActivity.h"
@implementation PrintActivity
- (NSString *)activityType
{
return @"MetriScan.Print";
}
- (NSString *)activityTitle
{
return @"Print MtriScan";
}
- (UIImage *)activityImage
{
//***** Note: I recommend using two sizes, as the iPad's UIActivity image size differs from
//***** the iPhone's. Also, create @2x sizes for Retina compatible devices. So you will
//***** have a total of 4 images.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return [UIImage imageNamed:@"test_72.png"];
}
return [UIImage imageNamed:@"test_57.png"];
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s", __FUNCTION__);
return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController
{
NSLog(@"%s",__FUNCTION__);
return nil;
}
- (void)performActivity
{
// This is where your custom print code should go
}
@end
不要忘记制作这两个文件:
PrintProvider.h
#import <UIKit/UIKit.h>
@interface PrintProvider : UIActivityItemProvider <UIActivityItemSource>
@end
PrintProvider.m
#import "PrintProvider.h"
@implementation PrintProvider
#pragma mark - UIActivityItemSource
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
NSLog(@"%s",__FUNCTION__);
NSLog(@"%@", activityType);
return [super activityViewController:activityViewController itemForActivityType:activityType];
}
@end
现在我们终于可以称之为:
- (IBAction)printReport:(UIBarButtonItem *)sender {
CustomProvider *customProvider =
[[CustomProvider alloc]init];
NSArray *items = [NSArray arrayWithObjects:customProvider,nil];
CustomActivity *ca = [[CustomActivity alloc]init];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:items
applicationActivities:[NSArray arrayWithObject:ca]];
activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo,
UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,UIActivityTypeMail,UIActivityTypePostToTwitter,
UIActivityTypePostToFacebook,UIActivityTypeMessage];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
{
NSLog(@" activityType: %@", activityType);
NSLog(@" completed: %i", completed);
};
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:activityVC];
CGRect rect = [[UIScreen mainScreen] bounds];
[self.popoverController
presentPopoverFromRect:rect inView:self.view
permittedArrowDirections:0
animated:YES];
}
答案 1 :(得分:3)
@ troop231 - 很棒的答案,非常有帮助。
我唯一要补充的是确保信号完成操作或完成例程不会被调用,并且popover不会被忽略。类似的东西:
- (void)performActivity {
NSLog(@"Do the actual activity here");
// My custom code here
[self activityDidFinish:YES]; // indicate completion here!!
}
答案 2 :(得分:-1)
图像的alpha通道用作生成遮罩的遮罩 呈现给用户的最终图像。图像中的任何颜色数据 本身被忽略了。不透明像素具有应用于它们的渐变 然后将此梯度放在标准背景之上。因此,一个 完全不透明的图像会产生渐变填充矩形。对于 iPhone和iPod touch,图像应不大于43乘43点 (对于具有Retina显示器的设备,相当于86 x 86像素。) 对于iPad,图像不应超过55 x 55点(其中 相当于带有Retina显示屏的iPad的110×110像素。)