iOS - 图片压缩

2017-05-27  本文已影响0人  HanZhiZzzzz

1,图片尺寸压缩

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth//图片压缩,第二个参数为最终宽度
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

方法举例:

  UIImage *newImage=[selfimageCompressForWidth:imagetargetWidth:image.size.width/2];

2,尺寸不变,大小压缩

举例:ASI上传图片时,

NSData *data = UIImagePNGRepresentation(image);//未压缩
NSData *data =UIImageJPEGRepresentation(image,0.5);//压缩

3,QBImagePickerController多选照片出现的问题

QBImagePickerController.m中方法
- (NSDictionary *)mediaInfoFromAsset:(ALAsset *)asset

3.1,取照片后自动调整为尺寸不超过屏幕宽高:

    [mediaInfo setObject:[UIImageimageWithCGImage:[[assetdefaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];

3.2,取照片后为照片原图:

        [mediaInfo setObject:[UIImageimageWithCGImage:[[assetdefaultRepresentation] fullResolutionImage]] forKey:@"UIImagePickerControllerOriginalImage"];

3.3,3.2中照片较大会出现左转90度,解决方法为:

    [mediaInfo setObject:[UIImageimageWithCGImage:[[assetdefaultRepresentation] fullResolutionImage]
                                             scale:[assetdefaultRepresentation].scale
                                       orientation:(UIImageOrientation)[assetdefaultRepresentation].orientation]forKey:@"UIImagePickerControllerOriginalImage"];

3中以上三点为并列语句。

摘自:http://blog.csdn.net/lichuanliangios/article/details/51488487

上一篇下一篇

猜你喜欢

热点阅读