您好,登錄后才能下訂單哦!
小編給大家分享一下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;}
以上是“iOS如何實現微信分享多張圖片功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。