iOS实现微信分享多张图片功能
前言
微信分享到朋友圈,可分享的类型有:文字类型、图片类型、音乐类型、视频类型和网页类型,但是我们在做图片分享的时候发现微信给的api只能分享一张图片,达不到一些app的需求,而产品汪或者boss想要分享多张图片,比如前段时间我做的一个app,是电商类app,想把商品的图片都分享到朋友圈,查看微信的api根本没有提供api,在这里我们只能用ios系统自带的分享了。
uiactivityviewcontroller
uiactivityviewcontroller是在ios 6开始支持的,同样是不能在apple tv的开发中使用。而且uiactivityviewcontroller是直接继承uiviewcontroller的,这意味着我们需要自己来展示和解散视图。
主要代码
oc代码
uiimage *imagetoshare = [uiimage imagenamed:@"99687078.jpg"]; uiimage *imagetoshare1 = [uiimage imagenamed:@"2222.jpg"]; nsarray *activityitems = @[imagetoshare,imagetoshare1]; uiactivityviewcontroller *activityvc = [[uiactivityviewcontroller alloc]initwithactivityitems:activityitems applicationactivities:nil]; [self presentviewcontroller:activityvc animated:true completion:nil];
swift代码
let imagetoshare1 = uiimage.init(named: "99687078.jpg") let imagetoshare2 = uiimage.init(named: "2222.jpg") let activityitems = [imagetoshare1,imagetoshare2] let activityvc = uiactivityviewcontroller.init(activityitems: activityitems, applicationactivities: nil) self.present(activityvc, animated: true, completion: nil)
运行截图
注意
我们在分享多张图片到朋友圈的时候会发现,我们在不做任何处理的时候,直接从手机相册调取多张图片分享的时候,分享失败。具体什么原因我研究了一下,发现分享的数据总共大小有限制;在这里举个例子,比如电梯,电梯限定的重量是固定的,所以不管你乘坐多少人,乘坐的人比较胖,那就乘坐的人比较少,如果乘坐的人比较瘦,那就可以多坐几个;所以uiactivityviewcontroller分享多张图片到朋友圈的时候,我根据图片数量的多少,然后把图片压缩到指定宽高,比如一张图片,我就压缩成和微信压缩图片一样的大小1280,如果9张我就压缩成500;
代码
压缩图片的方法在下面粘出,其中compressibilityfactor为图片宽高的最大值;
#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<imagearr.count; i++) { uiimage *newimg = [self getjpegimagerimg:imagearr[i]]; [newimgarr addobject:newimg]; } return newimgarr; } #pragma mark - 压缩一张图片 自定义最大宽高 - (uiimage *)getjpegimagerimg:(uiimage *)image compressibilityfactor:(cgfloat)compressibilityfactor{ cgfloat oldimg_wid = image.size.width; cgfloat oldimg_hei = image.size.height; //cgfloat aspectratio = oldimg_wid/oldimg_hei;//宽高比 if(oldimg_wid > 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(); return newimage; }
demo下载地址:ios实现微信分享多张图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 午饭的好处和坏处有什么