Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)saveImageToDisk:(UIImage *)newImage
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- UIImageOrientation orientation = [newImage imageOrientation];
- // Scale image first
- CGSize imageSize = [newImage size];
- BOOL imageIsRotated = NO;
- if(orientation == UIImageOrientationLeft
- || orientation == UIImageOrientationRight
- || orientation == UIImageOrientationLeftMirrored
- || orientation == UIImageOrientationRightMirrored)
- imageIsRotated = YES;
- float w1 = imageSize.width;
- float h1 = imageSize.height;
- if(imageIsRotated)
- {
- while(h1 > 640 || w1 > 480)
- {
- h1--;
- w1--;
- }
- }
- else
- {
- while(w1 > 640 || h1 > 480)
- {
- h1--;
- w1--;
- }
- }
- float s1 = (float)h1 / imageSize.height;
- float s2 = (float)w1 / imageSize.width;
- float scale = (s1 > s2 ? s1 : s2);
- // Draw scaled version..
- int w = imageSize.width * scale;
- int h = imageSize.height * scale;
- if(imageIsRotated)
- {
- w = imageSize.height * scale;
- h = imageSize.width * scale;
- }
- int larger = w;
- if(h > larger)
- larger = h;
- CGContextRef ctx = CGBitmapContextCreate(NULL, larger, larger, 8, larger * 4, colorSpace, kCGImageAlphaPremultipliedLast);
- configureCTM(ctx, orientation, CGSizeMake(larger, larger));
- CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), [newImage CGImage]);
- CGImageRef scaledImage = CGBitmapContextCreateImage(ctx);
- CGContextRelease(ctx);
- CGRect imageRect = CGRectMake(0, 0, w, h);
- if(imageIsRotated)
- imageRect = CGRectMake(0, 0, h, w);
- CGImageRef transformedImage = CGImageCreateWithImageInRect(scaledImage, imageRect);
- ctx = CGBitmapContextCreate(NULL, PCMaxImageSize.width, PCMaxImageSize.height, 8, PCMaxImageSize.width * 4, colorSpace, kCGImageAlphaPremultipliedLast);
- CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
- CGContextFillRect(ctx, CGRectMake(0, 0, PCMaxImageSize.width, PCMaxImageSize.height));
- float xt = (PCMaxImageSize.width - imageRect.size.width) / 2.0;
- float yt = (PCMaxImageSize.height - imageRect.size.height) / 2.0;
- CGContextDrawImage(ctx, CGRectMake(xt, yt, imageRect.size.width, imageRect.size.height), transformedImage);
- CGImageRef finalImage = CGBitmapContextCreateImage(ctx);
- fullImage = [[UIImage imageWithCGImage:finalImage] retain];
- CGContextRelease(ctx);
- CGImageRelease(scaledImage);
- CGImageRelease(transformedImage);
- CGImageRelease(finalImage);
- CGColorSpaceRelease(colorSpace);
- [pool drain];
- }
Add Comment
Please, Sign In to add comment