iOS 截屏图片分享到微信

释放双眼,带上耳机,听听看~!

微信分享存在图片不能超过32k的问题,这里我们主要解决图片截屏、压缩、分享的问题。

截屏

- (UIImage *)screenshot:(UIViewController *) controller
{
    CGRect rect = controller.view.bounds;
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == NULL) return nil;
    CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
    
    [self layoutIfNeeded];
    [[controller.view layer] renderInContext:context];
    
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshotImage;
}

压缩

+ (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
    // Compress by quality
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    if (data.length < maxLength) return image;
    
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    UIImage *resultImage = [UIImage imageWithData:data];
    if (data.length < maxLength) return resultImage;
    
    // Compress by size
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, compression);
    }
    
    return resultImage;
}

微信图片要求小于32k

WXMediaMessage *message = [WXMediaMessage message];
    // 设置消息缩略图的方法
    [message setThumbImage:[UIImage imageNamed:@"thumImage.png"]];
    // 多媒体消息中包含的图片数据对象
    WXImageObject *imageObject = [WXImageObject object];
    
    UIImage *image = [UIImage imageNamed:@"要分享的图片名"];
    NSUInteger maxSize  = 32*1024;
    image = [SnsShare compressImage:image  toByte:maxSize];

    // 图片真实数据内容
    
            NSData *data = UIImagePNGRepresentation(image);
    imageObject.imageData = data;
    // 多媒体数据对象,可以为WXImageObject,WXMusicObject,WXVideoObject,WXWebpageObject等。
    message.mediaObject = imageObject;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;// 分享到朋友圈
   if([WXApi sendReq:req])
    {
         NSLog(@"ok,it's successful !");
   }

人已赞赏
iOS文章

GCD实现多个定时器,完美避过NSTimer的三大缺陷(RunLoop、Thread、Leaks)

2019-10-26 11:11:43

iOS文章

用代码探讨 KVC/KVO 的实现原理

2019-10-26 13:11:29

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索