Skip to content

Instantly share code, notes, and snippets.

@datwelk
Created August 6, 2012 15:06
Show Gist options
  • Select an option

  • Save datwelk/3275142 to your computer and use it in GitHub Desktop.

Select an option

Save datwelk/3275142 to your computer and use it in GitHub Desktop.
- (void)start
{
NSData *imageData = [self dataFromImage:self.image];
//NSInteger maxBytes = 3145728;
NSInteger maxBytes = 30000;
NSInteger compressedSize = imageData.length;
NSLog(@"Compressed size: %i", compressedSize);
NSInteger originalWidth = CGImageGetWidth(self.image.CGImage);
NSInteger originalHeight = CGImageGetHeight(self.image.CGImage);
NSInteger bitsPerPixel = CGImageGetBitsPerPixel(self.image.CGImage);
NSInteger bytesPerPixel = bitsPerPixel / 8;
if (compressedSize > maxBytes)
{
//NSInteger uncompressedSize = (bytesPerPixel * originalWidth) * originalHeight;
NSInteger uncompressedSize = UIImagePNGRepresentation(self.image).length;
float compressieFactor = (float)uncompressedSize / (float)compressedSize;
maxBytes = maxBytes * compressieFactor;
NSInteger tooMuch = uncompressedSize - maxBytes;
float ratio = ((float)originalWidth / (float)originalHeight);
float heightFactor = (float)(ratio * bytesPerPixel);
float newTooMuch = (float)tooMuch / heightFactor;
NSInteger heightToDelete = sqrtf(newTooMuch);
NSInteger widthToDelete = ratio * heightToDelete;
self.image = [self.image photoByScalingForSize:CGSizeMake(originalWidth - widthToDelete, originalHeight - heightToDelete)];
imageData = [self dataFromImage:self.image];
}
}
- (NSData *)dataFromImage:(UIImage *)image
{
CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithData(destinationData, kUTTypeJPEG, 1, NULL);
CFDictionaryRef properties = (__bridge CFDictionaryRef)@{ (NSString *)kCGImageDestinationLossyCompressionQuality : [NSNumber numberWithFloat:1.0f]};
CGImageDestinationSetProperties(destinationRef, properties);
if (destinationRef != nil) {
CGImageDestinationAddImage(destinationRef, image.CGImage, nil);
if (CGImageDestinationFinalize(destinationRef))
{
NSData *imageData = [NSData dataWithData:(__bridge NSData *)destinationData];
CFRelease(destinationData);
CFRelease(destinationRef);
return imageData;
}
CFRelease(destinationData);
CFRelease(destinationRef);
}
CFRelease(destinationData);
return nil;
}
//Method in UIImage category
static inline double radians (double degrees) {return degrees * M_PI/180;}
- (UIImage *)photoByScalingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment