Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- + (CGBitmapInfo)normalizeBitmapInfo:(CGBitmapInfo)oldBitmapInfo {
- //extract the alpha info by resetting everything else
- CGImageAlphaInfo alphaInfo = oldBitmapInfo & kCGBitmapAlphaInfoMask;
- //Since iOS8 it's not allowed anymore to create contexts with unmultiplied Alpha info
- if (alphaInfo == kCGImageAlphaLast) {
- //alphaInfo = kCGImageAlphaPremultipliedLast;
- alphaInfo = kCGImageAlphaNoneSkipLast;
- }
- if (alphaInfo == kCGImageAlphaFirst) {
- //alphaInfo = kCGImageAlphaPremultipliedFirst;
- alphaInfo = kCGImageAlphaNoneSkipFirst;
- }
- //reset the bits
- CGBitmapInfo newBitmapInfo = oldBitmapInfo & ~kCGBitmapAlphaInfoMask;
- //set the bits to the new alphaInfo
- newBitmapInfo |= alphaInfo;
- return newBitmapInfo;
- }
- + (CGImageRef)getImageRefWithImage:(UIImage *)image withSize:(CGSize)size
- {
- int W = size.width;
- int H = size.height;
- int W0 = image.size.width;
- int H0 = image.size.height;
- CGImageRef imageRef = image.CGImage;
- CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
- CGBitmapInfo bitmapInfo = [self normalizeBitmapInfo:CGImageGetBitmapInfo(imageRef)];
- CGContextRef bitmap = CGBitmapContextCreate(NULL, W, H, 8, 4 * W, colorSpaceInfo, bitmapInfo);
- if (bitmap == nil) {
- UIGraphicsBeginImageContext(size);
- [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
- UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- imageRef = destImage.CGImage;
- colorSpaceInfo = CGImageGetColorSpace(imageRef);
- bitmapInfo = [self normalizeBitmapInfo:CGImageGetBitmapInfo(imageRef)];
- bitmap = CGBitmapContextCreate(NULL, W, H, 8, 4 * W, colorSpaceInfo, bitmapInfo);
- }
- if(image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight){
- W = size.height;
- H = size.width;
- W0 = image.size.height;
- H0 = image.size.width;
- }
- double ratio = MAX(W/(double)W0, H/(double)H0);
- W0 = ratio * W0;
- H0 = ratio * H0;
- int dW = abs((W0-W)/2);
- int dH = abs((H0-H)/2);
- switch (image.imageOrientation) {
- case UIImageOrientationLeft:
- {
- CGContextRotateCTM (bitmap, M_PI/2);
- CGContextTranslateCTM (bitmap, 0, -H);
- break;
- }
- case UIImageOrientationRight:
- {
- CGContextRotateCTM (bitmap, -M_PI/2);
- CGContextTranslateCTM (bitmap, -W, 0);
- break;
- }
- case UIImageOrientationUp:
- {
- break;
- }
- case UIImageOrientationDown:
- {
- CGContextTranslateCTM (bitmap, W, H);
- CGContextRotateCTM (bitmap, -M_PI);
- break;
- }
- case UIImageOrientationLeftMirrored:
- {
- CGContextTranslateCTM (bitmap, W / 2, H / 2);
- CGContextRotateCTM (bitmap, M_PI/2);
- CGContextScaleCTM(bitmap, -1, 1);
- CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
- break;
- }
- case UIImageOrientationRightMirrored:
- {
- CGContextTranslateCTM (bitmap, W / 2, H / 2);
- CGContextRotateCTM (bitmap, -M_PI/2);
- CGContextScaleCTM(bitmap, -1, 1);
- CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
- break;
- }
- case UIImageOrientationUpMirrored:
- {
- CGContextTranslateCTM (bitmap, W / 2, H / 2);
- CGContextScaleCTM(bitmap, -1, 1);
- CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
- break;
- }
- case UIImageOrientationDownMirrored:
- {
- CGContextTranslateCTM (bitmap, W / 2, H / 2);
- CGContextScaleCTM(bitmap, 1, -1);
- CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
- break;
- }
- default:
- break;
- }
- CGRect finalRect = CGRectMake(-dW, -dH, W0, H0);
- CGContextDrawImage(bitmap, finalRect, imageRef);
- CGImageRef ref = CGBitmapContextCreateImage(bitmap);
- CGContextRelease(bitmap);
- return ref;
- }
- BOOL CGImageWriteToFile(CGImageRef image, NSString *path)
- {
- CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
- CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
- if (!destination) {
- NSLog(@"Failed to create CGImageDestination for %@", path);
- return NO;
- }
- CGImageDestinationAddImage(destination, image, nil);
- if (!CGImageDestinationFinalize(destination))
- {
- NSLog(@"Failed to write image to %@", path);
- CFRelease(destination);
- CGImageRelease(image);
- return NO;
- }
- CFRelease(destination);
- CGImageRelease(image);
- NSLog(@"Image saved at: %@", path);
- return YES;
- }
- + (void)saveImageWithPath:(NSString *)path withImage:(UIImage *)image withSize:(CGSize)size {
- CGImageWriteToFile([self getImageRefWithImage:image withSize:size], path);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement