图片压缩上传的方法集锦

#pragma mark 图片压缩处理

-(NSData*)resetSizeOfImageData:(UIImage*)sourceImage maxSize:(NSInteger)maxSize

{

//优先调整分辨率

CGSize newSize=CGSizeMake(sourceImage.size.width, sourceImage.size.height);

CGFloat tempHeight=newSize.height/1024;

CGFloat tempWidth=newSize.width/1024;

if (tempWidth>1.0&&tempWidth>tempHeight) {

newSize=CGSizeMake(sourceImage.size.width/tempWidth, sourceImage.size.height/tempHeight);

}

UIGraphicsBeginImageContext(newSize);

[sourceImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//再调整大小

NSData *imageData=UIImageJPEGRepresentation(newImage, 0.4);

NSUInteger sizeOrigin=[imageData length];

NSUInteger sizeOriginKB=sizeOrigin/1024;

if (sizeOriginKB>maxSize) {

NSData *finalImageData=UIImageJPEGRepresentation(newImage, 0.5);

return finalImageData;

}

return imageData;

}

你可能感兴趣的:(图片压缩上传的方法集锦)