UIImageWriteToSavedPhotosAlbum creates black thumbnail in camera roll - iphone

I am saving image in the camera roll using UIImageWriteToSavedPhotosAlbum but always get an black thumbnail even if the picture is correct.
Do you have pointers to address this?
Thanks in advance for your help.
Regards,

I had the same problem. Drawing the image within a UIGraphicsImageContext solves the issue:
CGRect rect = CGRectMake(0,0,100,100);
UIImage *image = ((put here your image));
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(
result, self, #selector(image:didFinishSavingWithError:contextInfo:),nil);

Related

Maintain image resolution in screen grab

In my app, the user is able to put stickers on top of a photo. When they go to save their creation, I do a screen grab and store it in a UIImage:
UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
(where self.mainView has a subview UIImageView which holds the photo, and another subview UIView which holds the stickers).
I am wondering, is it possible to do a screen shot in this manner, and maintain the resolution of the aforementioned photo?
The following will 'flatten' two UIImages into one while maintaining the resolution of the original image(s):
CGSize photoSize = photoImage.size;
UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0);
CGRect photoRect = CGRectMake(0, 0, photoSize.width, photoSize.height);
// Add the original photo into the context
[photoImage drawInRect:photoRect];
// Add the sticker image with its upper left corner set to where the user placed it
[stickerImage drawAtPoint:stickerView.frame.origin];
// Get the resulting 'flattened' image
UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The above assumes photoImage and stickerImage are both instances of UIImage and stickerView is a UIView with containing the stickerImage and thus will be able to use the stickerView frame to determine its origin.
If you have multiple stickers, just iterate through the collection.
If you are looking to save an image of your current view then this might help you.
UIGraphicsBeginImageContext(self.scrollView.contentSize);
[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(finalImage.CGImage,
CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y,
scrollView.frame.size.width, scrollView.frame.size.height));
UIImage *screenImage = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
CGImageRelease(imageRef);

How to resize image with small size from camera

Do you have anyway to resize image from camera to small size. I would like to resize image to 612 * 612 with disk size is less than 100KB. This will help me to show image on application faster.
Any advise ?
Look at this post for help:
The simplest way to resize an UIImage?
and check out Apple's documentation on UIImage
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html
Try with this:
-(UIImage *)imageWithImage:(UIImage *)imageToCompress scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[imageToCompress drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Give your file name istead of getPath & call the above:
UIImage *img = [UIImage imageWithContentsOfFile:getPath];
UIImage *imageToPass = [self imageWithImage:img scaledToSize:CGSizeMake(612.0, 612.0)];

How to scale image to 0.25 or resize image to 478*640 in iOS6

In my application i have image with dimension 1936*2592 and size upto 6 MB.
i need scale image to 0.25 or resize image to 478*640 and then upload it to server.
I have tried lots of ways to do it in ios6 but failed
imageData= UIImagePNGRepresentation(self.cl_PriviewImage);
UIImage *temp = [[UIImage alloc] initWithData: imageData];
ScaledImage = [temp resizedImage:CGSizeMake(478, 640) interpolationQuality:kCGInterpolationHigh];
ScaledData = UIImagePNGRepresentation( ScaledImage);
//self.cl_PriviewImage is my image
it shows resizedImage method not found.
I have also tried with ScaleToSize method but its showing same error.
i have also tried with
ScaledImage = [UIImage imageWithCGImage:self.cl_PriviewImage.CGImage scale:0.25f orientation:self.cl_PriviewImage.imageOrientation];
ScaledData = UIImagePNGRepresentation(ScaledImage);
in this case there is no error but my purpose didn't get fulfilled i.e uploaded image doesn't get changed in scale/size.
Is there any any other way in iOS 6 or am i wrong some where?
Please suggest me.
Thanks in advance
- (UIImage*) image:(UIImage *) image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContextWithOptions(newSize, NO, 1);
CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationHigh);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
You can also add this method in a UIImage category, that would be a better solution.

Draw on a UIImage

I'm attempting to create a UIImage and save it to the photo gallery, the image gets created but it's just a plane white with no color what so ever. Any ideas?
UIGraphicsBeginImageContext(CGSizeMake(320,420));
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIImage *image = [[UIImage alloc] init];
[image drawInRect:CGRectMake(0, 0, 100, 100)];
UIImage *newImage;
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, self, #selector(image:didFinishSavingWithError:ctx:), nil);
Also just to add to the above. I added this line:
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
And now I get a grey image, still no rectangle, but it's something different so thought it might help
What UIImage are you trying to show?, this code only initialized a space of memory but it's filled with nothing, use [UIImage imageNamed:#"..."]; where the string is some file the resources for your project. Then once you have opened and drawn the image in the context you will be able to draw a string or whatever over it.
Also make sure that you close the context once you have gotten the image from it.

How to take a screenshot of a part of an app's window?

I'm trying to save a screenshot of a part of the screen with all views and layers merged. The only way I know how to do this is with the code below.
The code below creates an 80x80 square, but with a top left corner at 0,0.
How can I cut a smaller rectangle out of a full page screenshot? For example I have a 320x480 full window screenshot, but I only need an 80x80 square centered at 160,240?
UIGraphicsBeginImageContext(smallView.frame.size);
[appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Thank you!
after getting the screenshot image crop the required part using this
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
Untested suggestion...
UIGraphicsBeginImageContext(smallView.frame.size);
[appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
CGRect cropRect = UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
CGRectMake(160.0,240.0,smallView.frame.size.width,smallView.frame.size.height);
CGImageRef croppedImageRef = CGImageCreateWithImageInRect(screenshot.CGImage,cropRect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:croppedImageRef];
CGImageRelease(croppedImageRef);
UIGraphicsEndImageContext();