Image Optimization Like Whatsapp And Instagram In Ios And Android
Task :- optimize image like Facebook and WhatApp does? Upload image to server with optimize size in android and ios without losing image quality and dimension. I have seen many cod
Solution 1:
Try this code:
+(UIImage*)scaleAndRotateImage:(UIImage*)image {
int kMaxResolution =640; // Or whateverCGImageRef imgRef = image.CGImage;
CGFloat width =CGImageGetWidth(imgRef);
CGFloat height =CGImageGetHeight(imgRef);
CGAffineTransform transform =CGAffineTransformIdentity;
CGRect bounds =CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
CGFloat ratio = width/height;
if (ratio >1) {
bounds.size.width = kMaxResolution;
bounds.size.height = roundf(bounds.size.width / ratio);
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = roundf(bounds.size.height * ratio);
}
}
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize =CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch(orient) {
caseUIImageOrientationUp: //EXIF = 1
transform =CGAffineTransformIdentity;
break;
caseUIImageOrientationUpMirrored: //EXIF = 2
transform =CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform =CGAffineTransformScale(transform, -1.0, 1.0);
break;
caseUIImageOrientationDown: //EXIF = 3
transform =CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform =CGAffineTransformRotate(transform, M_PI);
break;
caseUIImageOrientationDownMirrored: //EXIF = 4
transform =CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform =CGAffineTransformScale(transform, 1.0, -1.0);
break;
caseUIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform =CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform =CGAffineTransformScale(transform, -1.0, 1.0);
transform =CGAffineTransformRotate(transform, 3.0*M_PI/2.0);
break;
caseUIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform =CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform =CGAffineTransformRotate(transform, 3.0*M_PI/2.0);
break;
caseUIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform =CGAffineTransformMakeScale(-1.0, 1.0);
transform =CGAffineTransformRotate(transform, M_PI/2.0);
break;
caseUIImageOrientationRight: //EXIF = 8
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform =CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform =CGAffineTransformRotate(transform, M_PI/2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context =UIGraphicsGetCurrentContext();
if (orient ==UIImageOrientationRight|| orient ==UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage*imageCopy =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGFloat scaleSize =0.2f;
UIImage*smallImage = [UIImage imageWithCGImage:imageCopy.CGImage
scale:scaleSize
orientation:imageCopy.imageOrientation];
return smallImage;
}
Solution 2:
This is what I am using in Android :
publicclassImageCompresser {
publicstaticvoidcompressImage(String inputFilePath, String outputFilePath) {
try {
final BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(inputFilePath, options);
//options.inSampleSize = calculateInSampleSize(options,400,300);
options.inSampleSize = calculateInSampleSize(options,800,1000);//Specify Minimum Height, Width of the resulting bitmap maintaining the aspect ratio
options.inJustDecodeBounds = false;
Bitmapbm= BitmapFactory.decodeFile(inputFilePath, options);
FileOutputStream fileOutputStream;
fileOutputStream = newFileOutputStream(outputFilePath);
ExifInterface exif;
try {
exif = newExifInterface(inputFilePath);
Log.d("EXIF", "Make : " + exif.getAttribute(ExifInterface.TAG_MAKE));
intorientation= exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif Orientation : " + orientation);
Matrixmatrix=newMatrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} elseif (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} elseif (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
BitmapscaledBitmap= Bitmap.createBitmap(bm, 0, 0,
bm.getWidth(), bm.getHeight(), matrix,
true);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);//Instead of 100, you can provide any value. But it will reduce the image quality
scaledBitmap.recycle();
} catch (IOException e) {
e.printStackTrace();
}
fileOutputStream.flush();
fileOutputStream.close();
bm.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
privatestaticintcalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
finalintimageWidth= options.outWidth;
finalintimageHeight= options.outHeight;
Log.d("Test", "imageWidth : " + imageWidth);
Log.d("Test", "imageHeight : " + imageHeight);
intinSampleSize=1;
if(imageWidth > reqWidth || imageHeight > reqHeight) {
finalinthalfWidth= imageWidth / 2;
finalinthalfHeight= imageHeight / 2;
while((halfWidth / inSampleSize) > reqWidth
&& (halfHeight / inSampleSize) > reqHeight) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
References :
Post a Comment for "Image Optimization Like Whatsapp And Instagram In Ios And Android"