今天就跟大家聊聊有關(guān)怎么在iOS中實現(xiàn)微信分享多張圖片功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站運營、企業(yè)網(wǎng)站設(shè)計、泗縣網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
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)
運行截圖
注意
我們在分享多張圖片到朋友圈的時候會發(fā)現(xiàn),我們在不做任何處理的時候,直接從手機相冊調(diào)取多張圖片分享的時候,分享失敗。具體什么原因我研究了一下,發(fā)現(xiàn)分享的數(shù)據(jù)總共大小有限制;在這里舉個例子,比如電梯,電梯限定的重量是固定的,所以不管你乘坐多少人,乘坐的人比較胖,那就乘坐的人比較少,如果乘坐的人比較瘦,那就可以多坐幾個;所以UIActivityViewController分享多張圖片到朋友圈的時候,我根據(jù)圖片數(shù)量的多少,然后把圖片壓縮到指定寬高,比如一張圖片,我就壓縮成和微信壓縮圖片一樣的大小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){ //超過設(shè)置的最大寬度 先判斷那個邊最長 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){ //超過設(shè)置的最大寬度 先判斷那個邊最長 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 - 根據(jù)寬高壓縮圖片 - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{ UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
看完上述內(nèi)容,你們對怎么在iOS中實現(xiàn)微信分享多張圖片功能有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章題目:怎么在iOS中實現(xiàn)微信分享多張圖片功能
轉(zhuǎn)載來于:http://jinyejixie.com/article16/ppiddg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、全網(wǎng)營銷推廣、外貿(mào)建站、手機網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)