i am sharing image on instagram
implementing below code. Please follow this code, i think this will help you for iOS
development.
In your .h file
add a Delegate Method
." UIDocumentInteractionControllerDelegate
"
And declare UIDocumentInteractionController *docController;
In your .m File
where you want to share the image on Instagram, write this code on click instagram
button action.
-(void)instagramshare_action
{
NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
NSFileManager *fileManager = [NSFileManager defaultManager] ;
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath=[path objectAtIndex:0];
imagePath =[imagePath stringByAppendingPathComponent:@"ResizeImage"];
NSArray *temp=[fileManager contentsOfDirectoryAtPath:imagePath error:nil];
NSString *tempImgPath = [imagePath stringByAppendingPathComponent:[temp objectAtIndex:0]];
NSData *imgData = [NSData dataWithContentsOfFile:tempImgPath];
UIImage *img = [UIImage imageWithData:imgData];
CGRect rect = CGRectMake(0,0 ,612,612);
[img drawInRect:rect];
CGRect cropRect5 = CGRectMake(0,0,612,612);
UIGraphicsBeginImageContextWithOptions(cropRect5.size,2.0,1.0);
[img drawInRect:cropRect5];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@" custom sc==%@",customScreenShot);
UIGraphicsEndImageContext();
//saving to document directory
NSArray *paths10 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory10 = [paths10 objectAtIndex:0];
NSString *ImageName10 = [NSString stringWithFormat:@"image.png"];
NSString* appFile = [documentsDirectory10 stringByAppendingPathComponent:ImageName10];
NSLog(@"appFile==%@",appFile);
NSData* myImageAsData = [NSData dataWithData:UIImagePNGRepresentation(customScreenShot)];
[myImageAsData writeToFile:appFile atomically:YES];
//getting image(png format) from document directory and again saving with ig format
NSString *ImageName1 =@"image.png";
NSString *getImagePath =[documentsDirectory10 stringByAppendingPathComponent:ImageName1];
UIImage *img1 = [UIImage imageWithContentsOfFile:getImagePath];
NSData *data = UIImageJPEGRepresentation(img1, 1.0);
UIImage *images = [[UIImage alloc] initWithData:data];
NSLog(@"img==%@",images);
CGRect rect2 = CGRectMake(0,0,612,612);
[img1 drawInRect:rect2];
NSString *ImageName = [NSString stringWithFormat:@"Image.ig"];
NSString* appFile2 = [documentsDirectory10 stringByAppendingPathComponent:ImageName];
NSLog(@"appFile==%@",appFile2);
NSData* myImageAsData2 = [NSData dataWithData:UIImagePNGRepresentation(img1)];
NSLog(@"myimagedata==%@",myImageAsData2);
NSURL *imageUrl = [NSURL fileURLWithPath:appFile2];
[myImageAsData2 writeToFile:appFile2 atomically:YES];
docController = [[UIDocumentInteractionController alloc] init];
docController.delegate = self;
[docController retain];
docController.UTI = @"com.instagram.photo";
[docController setURL:imageUrl];
CGRect rect4=CGRectMake(0,0,320,480);
[docController presentOpenInMenuFromRect:rect4 inView:self.view animated:YES];
}
else
{
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 show];
[errorToShare release];
}
}
Important : You have to install Instagram App on your device otherwise i write the code for showing alert. And for checking with `instagram` you have to create a account on `instagram`
I think this will help you, implementation for instagram
for iOS
. :)