怎么在iOS中实现微信分享多张图片功能
今天就跟大家聊聊有关怎么在iOS中实现微信分享多张图片功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
目前创新互联已为成百上千家的企业提供了网站建设、域名、网站空间、网站运营、企业网站设计、泗县网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
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; icompressibilityFactor || 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 看完上述内容,你们对怎么在iOS中实现微信分享多张图片功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
文章标题:怎么在iOS中实现微信分享多张图片功能
本文路径:http://azwzsj.com/article/ppiddg.html