save photo in webview to ios photo library - iphone

this is a case in which the photo is taken using camera and saved in library.
and the photo is opened in webpage for editing purpose and how to save back to device memory with another name? ie... Hw to save the photo displayed in webview to photo library?
Thanks in adv.

bellow method done your need:-
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"yourimageurl"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webV loadRequest:req];
UITapGestureRecognizer *gs = [[UITapGestureRecognizer alloc] init];
gs.numberOfTapsRequired = 1;
gs.delegate = self;
[self.view addGestureRecognizer:gs];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(#"TAPPED");
//Touch gestures below top bar should not make the page turn.
//EDITED Check for only Tap here instead.
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
CGPoint touchPoint = [touch locationInView:self.view];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
bool pageFlag = [userDefaults boolForKey:#"pageDirectionRTLFlag"];
NSLog(#"pageFlag tapbtnRight %d", pageFlag);
if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSString *imgURL = [NSString stringWithFormat:#"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
NSString *urlToSave = [webV stringByEvaluatingJavaScriptFromString:imgURL];
NSLog(#"urlToSave :%#",urlToSave);
NSURL * imageURL = [NSURL URLWithString:urlToSave];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
imgView.image = image;
}
}
return YES;
}
Download the demo:
http://dl.dropbox.com/u/51367042/ImageFromWebView.zip and cradit gose to this
Screenshot

read the tutorial from here. It will solve your problem

Related

How to Download a video using UIWebView

I want to download a the video using web-view but I am not getting how to download it?
my video link is here
the sample code for playing video which I am using is
-(void)embedYouTubeInWebView:(NSString*)url theWebView:(UIWebView *)aWebView {
   NSString* html = [NSString stringWithFormat:#"%#", url];
   [aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:html]]];
}
- (void)viewDidLoad
{
   [super viewDidLoad];
   
  [self embedYouTubeInWebView:#"http://player.vimeo.com/video/32983838" theWebView:myWebView];
   // Do any additional setup after loading the view, typically from a nib.
}
can anyone please help me to download this video?
To download a file i used the following Code. Hope it will work for you too.
- (IBAction)getFileFromFtpServer:(UIView *)sender
{
NSString *stringURL = #"http://player.vimeo.com/video/32983838";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.mpeg4"];
[urlData writeToFile:filePath atomically:YES];
}
}
Try this here your video is playing..
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 380)];
webView.delegate=self;
[webView setOpaque:NO];
webView.backgroundColor=[UIColor clearColor];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame=CGRectMake((width/2) - 10,(height/2) - 54, 20, 20);
activityIndicator.center=self.view.center;
[activityIndicator hidesWhenStopped];
[self playVideo];
}
-(void) playVideo
{
NSURL *url = [NSURL URLWithString:#"http://player.vimeo.com/video/32983838"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[activityIndicator startAnimating];
[webView addSubview:activityIndicator];
}
/* WebViewDidStartLoad */
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activityIndicator startAnimating];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
/* WebViewDidFinishLoad */
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
/* WebViewdidFailLoadWithError */
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
If I understand correctly and in response to above, google has shut down several "converter sites" due to copyright infringements: http://www.real.com/resources/youtube-to-mp3-converter There are other sites available to download video content from (again, assuming you're abiding by the copyright guidelines) : This lists five sites where you can find and download online videos free. To make sure a video is legal to download, look for licensing information about the video. Typically, videos with a “Creative Commons” license are legal to download, but may have limitations on how you use the video. http://www.real.com/resources/download-one-video

Exporting a photo to Instagram

I am trying to export a photo from my application to Instagram, using this code:
// Capture Screenshot
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,self.view.opaque,0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
dic.UTI = #"com.instagram.photo";
dic.annotation = [NSDictionary dictionaryWithObject:#"my caption" forKey:#"InstagramCaption"];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
} else {
NSLog(#"No Instagram Found");
}
But it is not working. It crashes with the following error:
2013-01-19 20:40:41.948 Ayat[12648:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
I am not sure I'm properly:
1- Saving jpgpath as the screenshot I took .
2- Passing the "dic" document to Instagram.
In my app, I have also export photo to Instagram. (Image must be larger than 612x612 size)
Try this code:
-(void)shareImageOnInstagram:(UIImage*)shareImage
{
//Remember Image must be larger than 612x612 size if not resize it.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.ig"];
NSData *imageData=UIImagePNGRepresentation(shareImage);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
UIDocumentInteractionController *docController=[[UIDocumentInteractionController alloc]init];
docController.delegate=self;
[docController retain];
docController.UTI=#"com.instagram.photo";
docController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:#"Image Taken via #App",#"InstagramCaption", nil];
[docController setURL:imageURL];
[docController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES]; //Here try which one is suitable for u to present the doc Controller. if crash occurs
}
else
{
NSLog (#"Instagram not found");
}
}
I hope this will helps you.
On this line
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
you are calling fileURLWithPath:. For a start this expects an NSString, not an NSURL, but from your error message it looks like igImageHookFile is nil.
It took a bunch of research but I finally found this at this site that helped. I just trimmed it up a bit to not include the caption as that was his original problem... but if you're only putting in the picture, here it is:
-(void)shareImageOnInstagram
{
//Remember Image must be larger than 612x612 size if not resize it.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]){
NSString *urlString = [[NSString alloc] initWithFormat:#"file://%#", blendedImageURL];
NSURL *imageUrl = [[NSURL alloc] initWithString: urlString];
self.docController = [self setupControllerWithURL:imageUrl usingDelegate:self];
self.docController.UTI = #"com.instagram.exclusivegram";
// self.docController.annotation = [NSDictionary dictionaryWithObject:#"I want to share this text" forKey:#"InstagramCaption"];
[self.docController presentOpenInMenuFromRect: self.view.frame inView: self.view animated: YES ];}
}
I tried the following code and it worked:
- (void) shareImageWithInstagram
{
NSURL *instagramURL = [NSURL URLWithString:#"instagram://"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
UICachedFileMgr* mgr = _gCachedManger;
UIImage* photoImage = [mgr imageWithUrl:_imageView.image];
NSData* imageData = UIImagePNGRepresentation(photoImage);
NSString* captionString = [NSString stringWithFormat:#"%#%#",self.approvedData.approvedCaption,self.approvedData.tag];
NSString* imagePath = [UIUtils documentDirectoryWithSubpath:#"image.igo"];
[imageData writeToFile:imagePath atomically:NO];
NSURL* fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"file://%#",imagePath]];
self.docFile = [[self setupControllerWithURL:fileURL usingDelegate:self]retain];
self.docFile.annotation = [NSDictionary dictionaryWithObject: captionString
forKey:#"InstagramCaption"];
self.docFile.UTI = #"com.instagram.photo";
// OPEN THE HOOK
[self.docFile presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}
else
{
[UIUtils messageAlert:#"Instagram not installed in this device!\nTo share image please install instagram." title:nil delegate:nil];
}
}

No audio played using MPMoviePlayerController

I have been using MPMoviePlayerController to play mp4 file. Video is playing fine but there is no audio at all.
I have downloaded first in one function and played using another. Please check the code below. Am I doing anything wrong here...
- (void) download_file : (NSString *) msg_url
{
if (msg_url != nil) {
// Download and play
NSURL *url = [NSURL URLWithString:msg_url];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
char file_path[512] = {0};
get_app_data_path(OFI_VC_MAILBOX_PATH, file_path); // Give document path.
NSString *filePath = [NSString stringWithFormat:#"%s/%#", file_path,#"mailbox.mp4"];
[urlData writeToFile:filePath atomically:YES];
[self performSelectorOnMainThread:#selector(play_mp4_file:) withObject:filePath waitUntilDone:NO];
}
}
}
- (void) play_mp4_file : (NSString *) filepath
{
if (is_same_view == true) {
[self.view setUserInteractionEnabled:YES];
if (filepath != nil) {
NSURL *file_url = [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:file_url];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
moviePlayer.useApplicationAudioSession = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}
}
Thanks for your helps
I have fixed the issue. Problem is with .mp4 extension, I have used .mov. Its playing audio now.

QLPreviewController - not displaying image from bundle?

This is my code... i've tried every path-format I can think of to get this to work... Basically I want to show an image in a quick and easy picture viewer... The viewer shows (modally... brilliant) but then the image dosen't show inside of that... I can't think why, the image definitely exists, and yet it just shows as a black screen.
thanks for any help. :)
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
QLPreviewController *preview = [[QLPreviewController alloc] init];
[preview setDataSource:self];
[self presentModalViewController:preview animated:YES];
}
#pragma mark QLPreviewController delegate methods
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {
return 1;
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {
NSString *imagePath = [NSString stringWithFormat:#"%#",[[NSBundle mainBundle] bundleURL]];
NSString *nextPath = [imagePath stringByAppendingPathComponent:imageName];
NSURL *imageURL = [NSURL URLWithString:nextPath];
return imageURL;
}
NSURL *imageURL = [NSURL URLWithString:nextPath];
[NSURL fileURLWithPath:nextPath];

how to add a image from the photo storage to a UIWebview

Is there anyway to display a image in a UIWebview from the iphone photo storage
May be this is the suitable code in the upload image,
- (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//we are listening out for links being click
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSURL *URL = [request URL]; //Get the URL
if ( [[URL scheme] isEqualToString:#"objc"] ) {
webView = webViewRef;
SEL method = NSSelectorFromString( [URL host] );
if ([self respondsToSelector:method])
{
[self performSelector:method withObject:nil afterDelay:0.1f];
}
return NO;
}
}
return YES;
}
-(void)takePicture
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
id delegate = [[UIApplication sharedApplication] delegate];
[[delegate viewController] presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
#pragma mark - Image Picker
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *flatImage = UIImageJPEGRepresentation(image, 0.1f);
NSString *image64 = [flatImage base64EncodedString];
NSString *js = [NSString stringWithFormat: #"processImage('data:image/jpeg;base64,%#')", image64];
[webView stringByEvaluatingJavaScriptFromString:js];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
in your image picker delegate, save the file locally, basic code snippet, not complete
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSData *data = UIImageJPEGRepresentation( [info objectForKey:#"UIImagePickerControllerOriginalImage"] , 1.0);
NSString *fullPath = [path stringByAppendingPathComponent:#"image_tmp.jpg"];
if ([[NSFileManager defaultManager] createFileAtPath:fullPath contents:data attributes:nil] ) {
NSLog([NSString stringWithFormat:#"file successfully written to path %#",fullPath],nil);
}
}
the in your webview, set the base path of the HTML page you are going to load to the same path used for saving the image file.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
If you just wanted to display the image, the code below works just fine (using the UIImagePickerDelegate)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[_youWebView loadData:UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]) MIMEType:#"image/jpeg" textEncodingName:#"utf-8" baseURL:nil];
}
hope this helps.
Yes, you first must use the UIImagePickerController. It will cause a new window from the iPhone to appear and the user will be requested to pick a photo.
When they pick the photo, you can get access to this photo, you can either dismiss the picker or the user can continue picking photos.
You are not allowed direct access to users photos and wont be able to access their photos with asking the user first. This makes total sense, as a user I would not want developers sneaking into my photo library and uploading my photos in the background.
To add these to a UIWebView, you will nee to set the [UIWebView basePath] to your (document directory where you saved the photo from the above process), I have got this working, search stackoverflow for UIWebView Basepath, or check documentation.
Cheers, John.