码迷,mamicode.com
首页 > 其他好文 > 详细

UIActivityViewController的相关用法

时间:2014-12-25 16:20:54      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

UIActivityViewController 是一个能为应用程序提供服务的试图控制器。操作系统已经提供了一些标准服务,诸如 复制内容到剪切板,提交内容到社交网站,通过 email 或 短信发送内容等。应用程序也可以自定义服务。

 

应用程序要复制配置、呈现和消除 UIActivityViewController。UIActivityViewContoller 的配置需要明确指出控制器要表现出的数据对象。使用代码如下:

- (IBAction)openActivitySheet:(id)sender
{
    //用实现了 UIActivityItemSource 协议的容器类生成 UIActivityViewController的实例,容器类的实例包含要发送的数据(此数据只是一个URL,文本等同之).
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.customURLContainer] applicationActivities:nil];
    // self.customURLContainer 是实现了 UIActivityItemSource 的容器实例,用来包含要发送的数据,关于此类下面会介绍
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        //iPhone直接展示即可
        [self presentViewController:activityViewController animated:YES completion:nil];
    }
    else
    {
        //iPad,用popOver展示
        if (![self.activityPopover isPopoverVisible]) {
            self.activityPopover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
            [self.activityPopover presentPopoverFromBarButtonItem:self.shareItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
        else
        {
            //已经展示则取消
            [self.activityPopover dismissPopoverAnimated:YES];
        }
    }
}

关于实现UIActivityItemSource的URL(文本)容器类,

 

#import <Foundation/Foundation.h>

@interface UrlContainer : NSObject<UIActivityItemSource>

@property (nonatomic, strong) NSURL *url;
-(instancetype)initWithUrl:(NSURL*)url;

@end

#import "UrlContainer.h"
#import "UIImage+resize.h"

@implementation UrlContainer

-(instancetype)initWithUrl:(NSURL *)url
{
    self = [super init];
    if(self)
    {
        self.url = url;
    }
    return self;
}

#pragma mark - UIActivityItemSource
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    // 占位,将要返回的类型
    return self.url;
}

-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    // 直接返回容器的url
    return self.url;
}
-(UIImage*)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size
{
    // 为item提供缩略图,大小为建议的大小
    // 用自定义的UIImage的Category resize 类方法处理图片为缩略图
    return [UIImage imageWithImage:[UIImage imageNamed:@"Beads.png"] scaledToFitToSize:size];
}
@end

自定义的UIImage+resize

#import <UIKit/UIKit.h>

@interface UIImage (resize)

+(UIImage*)imageWithImage:(UIImage*)image scaledToFitToSize:(CGSize)size;
+(UIImage*)imageWithImage:(UIImage*)image scaledToFillToSize:(CGSize)size;

@end

#import "UIImage+resize.h"

@implementation UIImage (resize)

// 根据尺寸和矩形重绘图片
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)size inRect:(CGRect)rect
{
    if ([UIScreen mainScreen].scale == 2.0) {
        UIGraphicsBeginImageContextWithOptions(size, YES, 2.0);
    }
    else
    {
        UIGraphicsBeginImageContext(size);
    }
    [image drawInRect:rect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

+(UIImage*)imageWithImage:(UIImage *)image scaledToFitToSize:(CGSize)size
{
    // 如果图片原尺寸已经小于要Scaled尺寸,不作处理
    if(image.size.height < size.height && image.size.width < size.width)
    {
        return [image copy];
    }
    
    // 根据缩放后的宽高,得到缩放比例
    CGFloat widthScaled = size.width / image.size.width;
    CGFloat heigtScaled = size.height / image.size.height;
    CGFloat scaledFactor = widthScaled > heigtScaled ? heigtScaled : widthScaled;
    
    // 缩放图片的尺寸
    CGSize scaledSize = CGSizeMake(image.size.width * scaledFactor, image.size.height * scaledFactor);
    return [UIImage imageWithImage:image scaledToSize:scaledSize inRect:CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height)];
}

+(UIImage*)imageWithImage:(UIImage *)image scaledToFillToSize:(CGSize)size
{
    // 如果图片原尺寸已经小于要Scaled尺寸,不作处理
    if(image.size.height < size.height && image.size.width < size.width)
    {
        return [image copy];
    }
    
    // 根据缩放后的宽高,得到缩放比例
    CGFloat widthScaled = size.width / image.size.width;
    CGFloat heigtScaled = size.height / image.size.height;
    CGFloat scaledFactor = widthScaled > heigtScaled ? heigtScaled : widthScaled;
    // 缩放图片的尺寸
    CGSize scaledSize = CGSizeMake(image.size.width * scaledFactor, image.size.height * scaledFactor);
    
    CGPoint scaledPoint;
    widthScaled > heigtScaled ? (scaledPoint.y = (size.height - scaledSize.height)/2) : (scaledPoint.x = (size.width - scaledSize.height)/2);
    
    return [UIImage imageWithImage:image scaledToSize:size inRect:CGRectMake(scaledPoint.x, scaledPoint.y, scaledSize.width, scaledSize.height)];
}

@end

 

UIActivityViewController的相关用法

标签:

原文地址:http://www.cnblogs.com/1oo1/p/4184707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!