UIImage Saving issue in iOS7 : Blending issue - iphone

Here is screenshot from simulator and saved image:
Code :
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//guru - just for simulator
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Image.png"];
[UIImagePNGRepresentation(screenshotImage) writeToFile:filePath atomically:YES];
//end guru
UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
How can I fix this problem ? Works good when I build it with Xcode4_iOS6, but not with Xcode5_iOS7.

You can fix it in this way:
if([self IS_IOS_7])
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, TRUE, [[UIScreen mainScreen] scale]);
}
else
{
UIGraphicsBeginImageContext(self.view.bounds.size);
}
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
iOS7 Check:
-(bool)IS_IOS_7
{
NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
float sysver = [currOsVersion floatValue] ;
if(sysver >=7.0f)
return true;
return false;
}

Related

how to convert a selected image from iphone library to pdf in ios sdk

Now i am using this code. Pdf is generating but image is not displaying.
-(void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"%#",info);
//selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#" selected image is - %#",image);
imageStr=[NSString stringWithFormat:#"%#",image];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath=[documentsDirectory stringByAppendingPathComponent:#"images.pdf"];
NSLog(#"file path is %#",localFilePath);
[self pdfGenerate:localFilePath];
UIGraphicsEndPDFContext();
[imagePicker dismissViewControllerAnimated:YES completion:nil];
}
-(void)pdfGenerate:(NSString *)filepath
{
UIGraphicsBeginPDFContextToFile(filepath, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 300, 300), nil);
[self convertImageToPdf];
}
-(void)convertImageToPdf
{
CGRect imagerect=CGRectMake(0, 0, 300, 300);
NSLog(#"image is %#",imageStr);
UIImage *image=[UIImage imageNamed:imageStr];
[image drawInRect:imagerect];
}
You are saving the selected image in NSString object imageStr (imageStr=[NSString stringWithFormat:#"%#",image];)
So when you are creating the image inside convertImageToPdf method (UIImage *image=[UIImage imageNamed:imageStr];), your image object is nil. so the pdf is blank.
Instead you can have UIImage reference to the image you picked from the picker. Use that reference inside convertImageToPdf to draw.
Update to your code:
Inside didFinishPickingMediaWithInfo
pickedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
Inside convertImageToPdf
CGRect imagerect=CGRectMake(0, 0, 300, 300);
[pickedImage drawInRect:imagerect];
pickedImage is an object of type UIImage.

Sharing an image to Instagram on iOS

I am trying to open one of my pictures on instagram but every time I push the action button (as you can see from my code) it shows instagram icon and when I push the Instagram icon the app crashes. What am I doing wrong? I have been stuck on this for a while.
interface ViewController : UIViewController <UIDocumentInteractionControllerDelegate>{
IBOutlet UIImageView *onlyImageVIew;
IBOutlet UIImageView *myImageView;
}
#property (nonatomic, retain) UIDocumentInteractionController *docController;
-(IBAction)actionButton:(id)sender;
#end
-(IBAction)actionButton:(id)sender {
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"Image.ig"];
UIImage *image = [UIImage imageNamed:#"01.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
NSURL *imageUrl = [NSURL fileURLWithPath:savedImagePath];
NSLog(#"%#",imageUrl);
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
docController.delegate = self;
docController.UTI = #"com.instagram.photo";
docController.URL = imageUrl;
//[docController setURL:imageUrl];
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
}
Here is the crash when I implemented the bool function
2013-02-10 13:34:46.206 share to instagram[2197:907] file://localhost/var/mobile/Applications/0AABBF7B-F479-44E7-BA7F-B0FAA636F1CB/Documents/Image.ig
hope below code works for you.
-(void)ShareInstagram
{
UIImagePickerController *imgpicker=[[UIImagePickerController alloc] init];
imgpicker.delegate=self;
[self storeimage];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
CGRect rect = CGRectMake(0 ,0 , 612, 612);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/15717.ig"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
dic.UTI = #"com.instagram.photo";
dic.delegate=self;
dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
dic.delegate=self;
[dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
// [[UIApplication sharedApplication] openURL:instagramURL];
}
else
{
// NSLog(#"instagramImageShare");
UIAlertView *errorToShare = [[UIAlertView alloc] initWithTitle:#"Instagram unavailable " message:#"You need to install Instagram in your device in order to share this image" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
errorToShare.tag=3010;
[errorToShare show];
}
}
- (void) storeimage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"15717.ig"];
UIImage *NewImg=[self resizedImage:imageCapture :CGRectMake(0, 0, 612, 612) ];
NSData *imageData = UIImagePNGRepresentation(NewImg);
[imageData writeToFile:savedImagePath atomically:NO];
}
-(UIImage*) resizedImage:(UIImage *)inImage: (CGRect) thumbRect
{
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
4 * thumbRect.size.width, // rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);
// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);
// Get an image from the context and a UIImage
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap); // ok if NULL
CGImageRelease(ref);
return result;
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate
{
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
interactionController.delegate = self;
return interactionController;
}
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
{
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action
{
// NSLog(#"5dsklfjkljas");
return YES;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action
{
// NSLog(#"dsfa");
return YES;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
{
// NSLog(#"fsafasd;");
}
This code works for me.
Best of Luck.
First, make sure your image isn't nil after the imageNamed: call.
Then, make sure the imageData isn't nil after the UIImagePNGRepresentation call.
Finally, check the result of writing the image to the file before attempting to use it:
BOOL writingResult = [imageData writeToFile:savedImagePath atomically:YES];
if(writingResult == NO)
{
NSLog(#"Failed to write to %#",savedImagePath);
return;
}
It's possible your file isn't being written, and so trying to share a file that doesn't exist is causing your crash.

Howe to capture UIView top UIView

i have UIView which displays the Graph..now I need to capture it to the UIImage and I googled it and got the below code.but if i use this in my code its not work.even if I use breakpoint compiler does not reach to this.I am using this in my UIView .where I am going wrong?
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
You can take a screenshot of any view or whole screen of the iPhone app with below method
- (UIImage *)captureView {
//hide controls if needed
CGRect rect = [self.view bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
And call it like bellow...
UIImage *tempImageSave=[self captureView];
and you can also save this image with this bellow line for photo album..
UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);
and you can also save this image with this bellow line for Document Directory..
NSData *imageData = UIImagePNGRepresentation(tempImageSave);
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:#"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
If the view contains the layer images or some graphics related data then use below method.
-(UIImage *)convertViewToImage:(UIView *)viewTemp
{
UIGraphicsBeginImageContext(viewTemp.bounds.size);
[viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
And use this method like below.
UIImage *tempImageSave = [self convertViewToImage:yourView];
I hope this helpful for you.
Try with this code it may be help you
- (UIImage *)captureView:(UIView *)view {
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
CGContextFillRect(ctx, screenRect);
[view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
you can save image of View like this way and store image in to Document Directory:-
-(void)SaveViewAsImage
{
UIGraphicsBeginImageContext(self.view.bounds.size);
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:#"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
}
Your image save in Document Directory :)
Download this demo
http://www.sendspace.com/file/gjxa82

Save one page of UIScrollView to image

I have an UIWebView. After loading data to UIWebView, my UIWebView can scroll horizontally, and i enable paging for my scrollView in web view.
My question is:
is there any way to save a page in scrollView in UIWebView to image? I mean, i will get 5 images for 5 pages of scrollView(content size = 5 pages)
you can save your screen like this way:-
-(IBAction)savebutn:(id)sender
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:#"%d.png",imagename];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
}
your Image will be save in your document directory with .png extension
This Code Useful when user want to capture current view ScreenShot and share or save this image....
- (UIImage *)captureView {
//hide controls if needed
CGRect rect = [self.view bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
see my blog,... http://parasjoshi3.blogspot.in/2012/06/captureimagescreenshot-of-view.html
:)
CGRect rect = [webview bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[webview.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This helps to capture the current image

convert uiview to .png image

I am working on iphone and I take the subclass of UIView and in draw rect method I am making some design. I want to convert this view in .png format.
Thanks in advance.
UIGraphicsBeginImageContext(myView.frame.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data=UIImagePNGRepresentation(viewImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strPath = [documentsDirectory stringByAppendingPathComponent:#"myimage.png"];
[data writeToFile:strPath atomically:YES];
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(YourView.frame.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(keyWindow.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
UIGraphicsEndImageContext();