繁体   English   中英

如何使用UIPasteboard异步加载图像URL以生成动画.gif

[英]How can I Asynchronously load an image URL using UIPasteboard for animated .gif

我的视图上有一个UIButton,这是按该按钮时的代码

- (IBAction)sendSMS:(UIButton *)sender 
{
    NSData *gifImage = [[NSData alloc] initWithContentsOfURL:url];

    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
    pasteBoard.persistent = YES;
    //pasteBoard.image = self.messageViewImage;
    [pasteBoard setData:gifImage forPasteboardType:@"com.compuserve.gif"];

    NSString *phoneToCall = @"sms:";
    NSString *phoneToCallEncoded = [phoneToCall     stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
    [[UIApplication sharedApplication] openURL:url];
}

我正在尝试复制动画gif,以粘贴到彩信中。 如何异步完成此任务? 当前,当我按下按钮时,大约需要3秒钟,并且按钮的背景为蓝色,然后弹出彩信。 如何为用户提供更好的体验?

实现目标的一种方法是使用MessageUI框架显示MFMessageComposeViewController ,并将GIF添加为附件。 使用这种方法,您无需将用户切换到“消息”应用程序-所有这些都在您自己的应用程序中完成。 这是您的操作方式。

第一步是将以下框架添加到您的项目中:

  • MessageUI.framework
  • MobileCoreServices.framework

在视图控制器中,添加以下导入:

#import <MessageUI/MessageUI.h>
#import <MobileCoreServices/UTCoreTypes.h>

接下来,为MFMessageComposeViewController定义属性:

@property (nonatomic, strong) MFMessageComposeViewController *messageController;

我们正在定义一个属性,以便以后可以处理用户在MMS视图中Cancel的情况。

在视图控制器的viewDidLoad ,添加以下内容:

self.messageController = [[MFMessageComposeViewController alloc] init];
self.messageController.messageComposeDelegate = self;

您希望您的sendSMS方法如下所示:

- (IBAction)sendSMS:(UIButton *)sender 
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSData *gifData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

        if ([MFMessageComposeViewController canSendText]) {
            [self.messageController addAttachmentData:gifData typeIdentifier:(__bridge NSString *)kUTTypeGIF filename:@"animated.gif"];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController: self.messageController animated:YES completion:NULL];
            });
        }

        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    });
}

在这种方法中,GIF在后台下载并分配给gifData 然后将gifData作为附件添加到消息gifData器视图,并向用户显示该gifData器视图。

当用户发送MMS或Cancel按钮时,将调用messageComposeViewController:didFinishWithResult: 在这种方法中,您需要关闭消息编写器模式视图:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self.smsComposer dismissViewControllerAnimated:YES completion:NULL];
}

根据用户的操作, result参数将为MessageComposeResultCancelledMessageComposeResultSentMessageComposeResultFailed

我不知道它是否会有所帮助,因为加载图像所需的时间不会因为您异步执行而缩短。 但是,这是异步的方法。

dispatch_queue_t que = dispatch_queue_create("myque", DISPATCH_QUEUE_SERIAL);
dispatch_async(que, ^{

    NSData *gifImage = [[NSData alloc] initWithContentsOfURL:url];

    dispatch_async(dispatch_get_main_queue(), ^{

        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        pasteBoard.persistent = YES;
        //pasteBoard.image = self.messageViewImage;
        [pasteBoard setData:gifImage forPasteboardType:@"com.compuserve.gif"];

    });
});

NSString *phoneToCall = @"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];

这里重要的是您要记住执行所有在主队列上更新GUI的代码,否则将不会更新GUI。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM