[ iOS ] Wechat sharing picture problem solved

When sharing on WeChat, the size of the image must be less than 32k, so in most cases, the image needs to be compressed and cropped. I have tried many methods and all are not right. Finally, the following method was used to successfully solve the problem.

// ------这种方法对图片既进行压缩,又进行裁剪
- (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImageJPEGRepresentation(newImage, 0.8);
}

// ------调用
UIImage *img = [UIImage imageWithData:[self imageWithImage:image scaledToSize:CGSizeMake(300, 300)]]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326244613&siteId=291194637