Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
- // Log out the image size
- NSLog(@"%lu KB",(imageData.length/1024));
- // Check if the image size is too large
- if ((imageData.length/1024) >= 1024) {
- while ((imageData.length/1024) >= 1024) {
- NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024)));
- // While the imageData is too large scale down the image
- // Get the current image size
- CGSize currentSize = CGSizeMake(image.size.width, image.size.height);
- // Resize the image
- image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality];
- // Pass the NSData out again
- imageData = UIImageJPEGRepresentation(image, kMESImageQuality);
- }
- }
- NSData *imageData = UIImageJPEGRepresentation(image, kMESImageQuality);
- double factor = 1.0;
- double adjustment = 1.0 / sqrt(2.0); // or use 0.8 or whatever you want
- CGSize size = image.size;
- CGSize currentSize = size;
- UIImage *currentImage = image;
- while (imageData.length >= (1024 * 1024))
- {
- factor *= adjustment;
- currentSize = CGSizeMake(roundf(size.width * factor), roundf(size.height * factor));
- currentImage = [image resizedImage:currentSize interpolationQuality:kMESImageQuality];
- imageData = UIImageJPEGRepresentation(currentImage, kMESImageQuality);
- }
- // Check if the image size is too large
- if ((imageData.length/1024) >= 1024) {
- while ((imageData.length/1024) >= 1024) {
- NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024)));
- // While the imageData is too large scale down the image
- // Get the current image size
- CGSize currentSize = CGSizeMake(image.size.width, image.size.height);
- // Resize the image
- image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality];
- // Pass the NSData out again
- imageData = UIImageJPEGRepresentation(image, kMESImageQuality);
- }
- }
- // Returns a rescaled copy of the image, taking into account its orientation
- // The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
- - (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
- BOOL drawTransposed;
- switch (self.imageOrientation) {
- case UIImageOrientationLeft:
- case UIImageOrientationLeftMirrored:
- case UIImageOrientationRight:
- case UIImageOrientationRightMirrored:
- drawTransposed = YES;
- break;
- default:
- drawTransposed = NO;
- }
- return [self resizedImage:newSize
- transform:[self transformForOrientation:newSize]
- drawTransposed:drawTransposed
- interpolationQuality:quality];
- }
- // Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
- // The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
- // If the new size is not integral, it will be rounded up
- - (UIImage *)resizedImage:(CGSize)newSize
- transform:(CGAffineTransform)transform
- drawTransposed:(BOOL)transpose
- interpolationQuality:(CGInterpolationQuality)quality {
- CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
- CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
- CGImageRef imageRef = self.CGImage;
- // Build a context that's the same dimensions as the new size
- CGContextRef bitmap = CGBitmapContextCreate(NULL,
- newRect.size.width,
- newRect.size.height,
- CGImageGetBitsPerComponent(imageRef),
- 0,
- CGImageGetColorSpace(imageRef),
- CGImageGetBitmapInfo(imageRef));
- // Rotate and/or flip the image if required by its orientation
- CGContextConcatCTM(bitmap, transform);
- // Set the quality level to use when rescaling
- CGContextSetInterpolationQuality(bitmap, quality);
- // Draw into the context; this scales the image
- CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
- // Get the resized image from the context and a UIImage
- CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
- UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
- // Clean up
- CGContextRelease(bitmap);
- CGImageRelease(newImageRef);
- return newImage;
- }
- // Returns an affine transform that takes into account the image orientation when drawing a scaled image
- - (CGAffineTransform)transformForOrientation:(CGSize)newSize {
- CGAffineTransform transform = CGAffineTransformIdentity;
- switch (self.imageOrientation) {
- case UIImageOrientationDown: // EXIF = 3
- case UIImageOrientationDownMirrored: // EXIF = 4
- transform = CGAffineTransformTranslate(transform, newSize.width, newSize.height);
- transform = CGAffineTransformRotate(transform, M_PI);
- break;
- case UIImageOrientationLeft: // EXIF = 6
- case UIImageOrientationLeftMirrored: // EXIF = 5
- transform = CGAffineTransformTranslate(transform, newSize.width, 0);
- transform = CGAffineTransformRotate(transform, M_PI_2);
- break;
- case UIImageOrientationRight: // EXIF = 8
- case UIImageOrientationRightMirrored: // EXIF = 7
- transform = CGAffineTransformTranslate(transform, 0, newSize.height);
- transform = CGAffineTransformRotate(transform, -M_PI_2);
- break;
- default:
- break;
- }
- switch (self.imageOrientation) {
- case UIImageOrientationUpMirrored: // EXIF = 2
- case UIImageOrientationDownMirrored: // EXIF = 4
- transform = CGAffineTransformTranslate(transform, newSize.width, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
- case UIImageOrientationLeftMirrored: // EXIF = 5
- case UIImageOrientationRightMirrored: // EXIF = 7
- transform = CGAffineTransformTranslate(transform, newSize.height, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
- default:
- break;
- }
- return transform;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement