保存gif图到相册和从相册获取gif图片

最近项目中用需要实现保存Gif 图到本地并可以分享.自己研究几天做出了,现在和大家分享一下吧

就是一个简单的demo,希望能够给有需要的人有一点帮助吧。写的不好,勿喷。

主要思路来自 ibireme 大神的博客 iOS 处理图片的一些小 Tip

如何把 GIF 动图保存到相册?

iOS 的相册是支持保存 GIF 和 APNG 动图的,只是不能直接播放。用 [ALAssetsLibrary writeImageDataToSavedPhotosAlbum:metadata:completionBlock] 可以直接把 APNG、GIF 的数据写入相册。如果图省事直接用 UIImageWriteToSavedPhotosAlbum() 写相册,那么图像会被强制转码为 PNG。

我这里用的是 ibireme的 YYImage
其实你项目中使用的是SDWebImage 直接使用 imageView sd_setImageWithURL: 即可显示Gif图, SDWebImage已经写了 UIImage+GIF分类

保存图片

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://up.toumiao.net/uploads/allimg/201509/29-090547_613.gif"]];

//把Image以data保存到相册
ALAssetsLibrary *assets = [[ALAssetsLibrary alloc]init];
[assets writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

}];

从相册获取Gif图

UIImagePickerController *pickCtrl = [[UIImagePickerController alloc]init];
//设置资源类型
pickCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickCtrl.delegate = self;

[self presentViewController:pickCtrl animated:YES completion:nil];

获取数据

NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
__weak typeof(self) weakSelf = self;
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    Byte *buffer = (Byte*)malloc((unsigned long)rep.size);
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:((unsigned long)rep.size) error:nil];
    NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    //********  获取到照片的data,在这 里进行上传等操作
    YYImage *image = [YYImage imageWithData:data];
    weakSelf.imageView.image = image;

} failureBlock:nil];

表情键盘

之前项目中的表情键盘,现在和大家分享一下吧,写的不好,不喜勿喷!

项目地址:EmotionKeyboard

效果图


直接把文件中的EmotionKeyboard文件添加到项目中即可,本项目依赖 MJExtension,需要注意的是项目资源文件直接 Create folder reference 形式引入.



使用方法

提供4种创建方法

1
2
3
4
5
6
7
EmotionKeyboard *emotionKeyboard = [[EmotionKeyboard alloc]initWithFrame:CGRectMake(0, ScreenH - 235, ScreenW, 235)];

EmotionKeyboard *emotionKeyboard = [[EmotionKeyboard alloc]init];

EmotionKeyboard *emotionKeyboard = [[EmotionKeyboard alloc]initWithDefault];

EmotionKeyboard *emotionKeyboard = [EmotionKeyboard sharedEmotionKeyboardView];

  • initWithFrame: 可自己指定键盘大小 已经做适配 默认为有四个选项
  • init: 默认Frame为 CGRectMake(0, ScreenH - 216, ScreenW, 216) 默认为有四个选项
  • sharedEmotionKeyboardView:单利模式 默认Frame为 CGRectMake(0, ScreenH - 216, ScreenW, 216) 默认为有四个选项
  • initWithDefault: 默认Frame为 CGRectMake(0, ScreenH - 216, ScreenW, 216) 只有默认的小表情


  • 默认创建大小自己可修改

    接口

    本项目是代理对外传值,只需成为代理 主要代理方法如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /**
    * 获取图片对应文字
    * @param text 文字
    */
    - (void)emoticonInputDidTapText:(NSString *)text;

    /**
    * 获取图片表情对应的url
    * @param url 图片路径
    */
    - (void)emoticonImageDidTapUrl:(NSString *)url;

    /**
    * 删除表情按键点击
    */

    - (void)emoticonInputDidTapBackspace;
    /**
    * 发送表情按钮点击
    */
    - (void)emoticonInputDidTapSend;

    扩展接口

    扩展接口都放到EmotionTool 文件中

  • 添加图片到收藏
  • 1
    2
    3
    4
    5
    6
    7
    8
    /**
    * 添加图片到收藏
    * @param url 图片url
    */
    + (void)addCollectImage:(NSString *)url;

    //使用
    [EmotionTool addCollectImage:url];

    如果需要替换默认表情,请替换资源文件. 并按照对应要去修改资源目录下info.plist文件

    默认素材来自于新浪微博

    具体使用方法 –参考demo

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 44)];
    textField.backgroundColor = [UIColor lightGrayColor];

    // EmotionKeyboard *emotionKeyBoard = [[EmotionKeyboard alloc]initWithFrame:CGRectMake(0, ScreenH - 235, ScreenW, 235)];

    // EmotionKeyboard *emotionKeyBoard = [[EmotionKeyboard alloc]init];

    // EmotionKeyboard *emotionKeyBoard = [[EmotionKeyboard alloc]initWithDefault];

    EmotionKeyboard *emotionKeyBoard = [EmotionKeyboard sharedEmotionKeyboardView];
    emotionKeyBoard.delegate = self;
    textField.inputView = emotionKeyBoard;
    _textField = textField;

    // FaceBoardDelegateMethods

    /**
    * 删除表情按钮点击
    */
    - (void)emoticonInputDidTapBackspace
    {
    NSString* inputString;
    inputString = _textField.text;
    NSString* string = nil;
    NSInteger stringLength = inputString.length;

    if (stringLength > 0) {
    if (stringLength == 1 || stringLength == 2) {//只有1个或2个字符时
    if ([inputString isEmoji]) {//emoji
    string = @"";
    }else {//普通字符
    string = [inputString substringToIndex:stringLength - 1];
    }
    }else if ([@"]" isEqualToString:[inputString substringFromIndex:stringLength - 1]]) {//默认表情

    if ([inputString rangeOfString:@"["].location == NSNotFound) {
    string = [inputString substringToIndex:stringLength - 1];
    }else {
    string = [inputString substringToIndex:[inputString rangeOfString:@"["options:NSBackwardsSearch].location];
    }
    }else if ([[inputString substringFromIndex:stringLength - 1] isEmoji] || [[inputString substringFromIndex:stringLength - 2] isEmoji]) {//末尾是emoji

    if ([[inputString substringFromIndex:stringLength - 2] isEmoji]) {
    string = [inputString substringToIndex:stringLength - 2];
    }else if ([[inputString substringFromIndex:stringLength - 1] isEmoji]) {
    string = [inputString substringToIndex:stringLength - 1];
    }
    }else {//是普通文字
    string = [inputString substringToIndex:stringLength - 1];
    }
    }
    [_textField setText:string];

    }

    /**
    * 发送表情按钮点击
    */
    - (void)emoticonInputDidTapSend
    {
    NSLog(@"发送");
    // [self sendComment];
    }

    /**
    * 获取表情对应字符
    *
    * @param text 表情对应字符串
    */
    - (void)emoticonInputDidTapText:(NSString*)text
    {
    [_textField insertText:text];
    }
    /**
    * 获取图片表情对应的url
    *
    * @param url
    */
    - (void)emoticonImageDidTapUrl:(NSString *)url{
    NSLog(@"%@",url);
    }