iOS 实现微信、微博多图分享

分享多图

目前观察的是分享到三方的很少支持多图分享的,然而观察到相册却能分享多图,不公平哎,不过才知道是支持使用的,那就是使用UIActivityViewController。

UIActivityViewController是在iOS 6开始支持的,同样是不能在Apple TV的开发中使用。而且UIActivityViewController是直接继承UIViewController的,这意味着我们需要自己来展示和解散视图。

1.OC版

UIImage *imageToShare = [UIImage imageNamed:@"1.jpg"];
UIImage *imageToShare1 = [UIImage imageNamed:@"1.jpg"];
NSArray *activityItems = @[imageToShare,imageToShare1];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:TRUE completion:nil];

2.Swift版

let imageToShare = UIImage.init(named: "1.jpg")  
let imageToShare1 = UIImage.init(named: "1.jpg")  
let activityItems = [imageToShare,imageToShare1]  
let activityVC = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)  
self.present(activityVC, animated: true, completion: nil)

注意

我们在分享多张图片到微信朋友圈的时候会发现,我们在不做任何处理的时候,直接从手机相册调取多张图片分享的时候,分享失败。具体什么原因我研究了一下,发现分享的数据总共大小有限制。所以我们要把图片压缩:

一、微信压缩算法推算

1.图片尺寸

宽高均 1280 && 宽高比 1280 && 宽高比 > 2 && 宽或高 1280 && 宽高比 > 2,取较小值等于1280,较大值等比例压缩

注:当宽和高均小于1280,并且宽高比大于2时,微信聊天会话和微信朋友圈的处理不一样。

朋友圈:取较小值等于1280,较大值等比例压缩

聊天会话:取较小值等于800,较大值等比例压缩

2.图片质量

经过大量的测试,微信的图片压缩质量值 ≈ 0.5

UIImageJPEGRepresentation(resizeImage, 0.5)

二、代码实现

#import "WeChatJPEGImager.h"  
#define KCompressibilityFactor 1280.00  
  
@implementation WeChatJPEGImager  
  
#pragma mark - 压缩一张图片 最大宽高1280 类似于微信算法  
- (UIImage *)getJPEGImagerImg:(UIImage *)image{  
    CGFloat oldImg_WID = image.size.width;  
    CGFloat oldImg_HEI = image.size.height;  
    //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比  
    if(oldImg_WID > KCompressibilityFactor || oldImg_HEI > KCompressibilityFactor){  
        //超过设置的最大宽度 先判断那个边最长  
        if(oldImg_WID > oldImg_HEI){  
            //宽度大于高度  
            oldImg_HEI = (KCompressibilityFactor * oldImg_HEI)/oldImg_WID;  
            oldImg_WID = KCompressibilityFactor;  
        }else{  
            oldImg_WID = (KCompressibilityFactor * oldImg_WID)/oldImg_HEI;  
            oldImg_HEI = KCompressibilityFactor;  
        }  
    }  
    UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];  
    NSData *dJpeg = nil;  
    if (UIImagePNGRepresentation(newImg)==nil) {  
        dJpeg = UIImageJPEGRepresentation(newImg, 0.5);  
    }else{  
        dJpeg = UIImagePNGRepresentation(newImg);  
    }  
    return [UIImage imageWithData:dJpeg];  
}  
#pragma mark - 压缩多张图片 最大宽高1280 类似于微信算法  
- (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr{  
    NSMutableArray *newImgArr = [NSMutableArray new];  
    for (int i = 0; i compressibilityFactor || oldImg_HEI > compressibilityFactor){  
        //超过设置的最大宽度 先判断那个边最长  
        if(oldImg_WID > oldImg_HEI){  
            //宽度大于高度  
            oldImg_HEI = (compressibilityFactor * oldImg_HEI)/oldImg_WID;  
            oldImg_WID = compressibilityFactor;  
        }else{  
            oldImg_WID = (compressibilityFactor * oldImg_WID)/oldImg_HEI;  
            oldImg_HEI = compressibilityFactor;  
        }  
    }  
    UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];  
    NSData *dJpeg = nil;  
    if (UIImagePNGRepresentation(newImg)==nil) {  
        dJpeg = UIImageJPEGRepresentation(newImg, 0.5);  
    }else{  
        dJpeg = UIImagePNGRepresentation(newImg);  
    }  
    return [UIImage imageWithData:dJpeg];  
}  
#pragma mark - 压缩多张图片 自定义最大宽高  
- (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr compressibilityFactor:(CGFloat)compressibilityFactor{  
    NSMutableArray *newImgArr = [NSMutableArray new];  
    for (int i = 0; i<imagearr.count; i++)="" {="" uiimage="" *newimg="[self" getjpegimagerimg:imagearr[i]="" compressibilityfactor:compressibilityfactor];="" [newimgarr="" addobject:newimg];="" }="" return="" newimgarr;="" #pragma="" mark="" -="" 根据宽高压缩图片="" (uiimage="" *)imagewithimage:(uiimage="" *)image="" scaledtosize:(cgsize)newsize{="" uigraphicsbeginimagecontext(newsize);="" [image="" drawinrect:cgrectmake(0,0,newsize.width,newsize.height)];="" uiimage*="" newimage="UIGraphicsGetImageFromCurrentImageContext();" uigraphicsendimagecontext();="" newimage;="" 
稿源:怼码人生 (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » iOS 实现微信、微博多图分享

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录