Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. + (CGBitmapInfo)normalizeBitmapInfo:(CGBitmapInfo)oldBitmapInfo {
  2.     //extract the alpha info by resetting everything else
  3.     CGImageAlphaInfo alphaInfo = oldBitmapInfo & kCGBitmapAlphaInfoMask;
  4.    
  5.     //Since iOS8 it's not allowed anymore to create contexts with unmultiplied Alpha info
  6.     if (alphaInfo == kCGImageAlphaLast) {
  7.         //alphaInfo = kCGImageAlphaPremultipliedLast;
  8.         alphaInfo = kCGImageAlphaNoneSkipLast;
  9.     }
  10.     if (alphaInfo == kCGImageAlphaFirst) {
  11.         //alphaInfo = kCGImageAlphaPremultipliedFirst;
  12.         alphaInfo = kCGImageAlphaNoneSkipFirst;
  13.     }
  14.    
  15.     //reset the bits
  16.     CGBitmapInfo newBitmapInfo = oldBitmapInfo & ~kCGBitmapAlphaInfoMask;
  17.    
  18.     //set the bits to the new alphaInfo
  19.     newBitmapInfo |= alphaInfo;
  20.    
  21.     return newBitmapInfo;
  22. }
  23.  
  24. + (CGImageRef)getImageRefWithImage:(UIImage *)image withSize:(CGSize)size
  25. {
  26.     int W  = size.width;
  27.     int H  = size.height;
  28.     int W0 = image.size.width;
  29.     int H0 = image.size.height;
  30.    
  31.     CGImageRef imageRef = image.CGImage;
  32.     CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
  33.     CGBitmapInfo bitmapInfo = [self normalizeBitmapInfo:CGImageGetBitmapInfo(imageRef)];
  34.     CGContextRef bitmap = CGBitmapContextCreate(NULL, W, H, 8, 4 * W, colorSpaceInfo, bitmapInfo);
  35.     if (bitmap == nil) {
  36.         UIGraphicsBeginImageContext(size);
  37.         [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  38.         UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
  39.         UIGraphicsEndImageContext();
  40.        
  41.         imageRef = destImage.CGImage;
  42.         colorSpaceInfo = CGImageGetColorSpace(imageRef);
  43.         bitmapInfo = [self normalizeBitmapInfo:CGImageGetBitmapInfo(imageRef)];
  44.         bitmap = CGBitmapContextCreate(NULL, W, H, 8, 4 * W, colorSpaceInfo, bitmapInfo);
  45.     }
  46.    
  47.     if(image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight){
  48.         W  = size.height;
  49.         H  = size.width;
  50.         W0 = image.size.height;
  51.         H0 = image.size.width;
  52.     }
  53.    
  54.     double ratio = MAX(W/(double)W0, H/(double)H0);
  55.     W0 = ratio * W0;
  56.     H0 = ratio * H0;
  57.    
  58.     int dW = abs((W0-W)/2);
  59.     int dH = abs((H0-H)/2);
  60.    
  61.     switch (image.imageOrientation) {
  62.         case UIImageOrientationLeft:
  63.         {
  64.             CGContextRotateCTM (bitmap, M_PI/2);
  65.             CGContextTranslateCTM (bitmap, 0, -H);
  66.             break;
  67.         }
  68.         case UIImageOrientationRight:
  69.         {
  70.             CGContextRotateCTM (bitmap, -M_PI/2);
  71.             CGContextTranslateCTM (bitmap, -W, 0);
  72.             break;
  73.         }
  74.         case UIImageOrientationUp:
  75.         {
  76.             break;
  77.         }
  78.         case UIImageOrientationDown:
  79.         {
  80.             CGContextTranslateCTM (bitmap, W, H);
  81.             CGContextRotateCTM (bitmap, -M_PI);
  82.             break;
  83.         }
  84.         case UIImageOrientationLeftMirrored:
  85.         {
  86.             CGContextTranslateCTM (bitmap, W / 2, H / 2);
  87.             CGContextRotateCTM (bitmap, M_PI/2);
  88.             CGContextScaleCTM(bitmap, -1, 1);
  89.             CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
  90.             break;
  91.         }
  92.         case UIImageOrientationRightMirrored:
  93.         {
  94.             CGContextTranslateCTM (bitmap, W / 2, H / 2);
  95.             CGContextRotateCTM (bitmap, -M_PI/2);
  96.             CGContextScaleCTM(bitmap, -1, 1);
  97.             CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
  98.             break;
  99.         }
  100.         case UIImageOrientationUpMirrored:
  101.         {
  102.             CGContextTranslateCTM (bitmap, W / 2, H / 2);
  103.             CGContextScaleCTM(bitmap, -1, 1);
  104.             CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
  105.             break;
  106.         }
  107.         case UIImageOrientationDownMirrored:
  108.         {
  109.             CGContextTranslateCTM (bitmap, W / 2, H / 2);
  110.             CGContextScaleCTM(bitmap, 1, -1);
  111.             CGContextTranslateCTM (bitmap, -W / 2, -H / 2);
  112.             break;
  113.         }
  114.            
  115.         default:
  116.             break;
  117.     }
  118.    
  119.     CGRect finalRect = CGRectMake(-dW, -dH, W0, H0);
  120.     CGContextDrawImage(bitmap, finalRect, imageRef);
  121.     CGImageRef ref = CGBitmapContextCreateImage(bitmap);
  122.    
  123.     CGContextRelease(bitmap);
  124.     return ref;
  125. }
  126.  
  127. BOOL CGImageWriteToFile(CGImageRef image, NSString *path)
  128. {
  129.     CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
  130.     CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
  131.     if (!destination) {
  132.         NSLog(@"Failed to create CGImageDestination for %@", path);
  133.         return NO;
  134.     }
  135.    
  136.     CGImageDestinationAddImage(destination, image, nil);
  137.     if (!CGImageDestinationFinalize(destination))
  138.     {
  139.         NSLog(@"Failed to write image to %@", path);
  140.        
  141.         CFRelease(destination);
  142.         CGImageRelease(image);
  143.        
  144.         return NO;
  145.     }
  146.    
  147.     CFRelease(destination);
  148.     CGImageRelease(image);
  149.     NSLog(@"Image saved at: %@", path);
  150.     return YES;
  151. }
  152.  
  153. + (void)saveImageWithPath:(NSString *)path withImage:(UIImage *)image withSize:(CGSize)size {
  154.     CGImageWriteToFile([self getImageRefWithImage:image withSize:size], path);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement