How to set image into uiwebview? - ios

I have created a html code like see above and the code uploaded to http://www.ged.Ic.com site.
Added an image name “1.jpg” into Xcode project.
When open iPad app loading http://www.ged.Ic.com/page/ from webview.
How to get to set image from into Xcode project to img src="image path from xcode"

NSString *imageFileName = #"image";
NSString *imageFileExtension = #"png";
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];
NSString *imgHTMLTag = [NSString stringWithFormat:#"<img src=\"file://%#\" />", imagePath];

NSString *imageUrlString = #"http://www.gstatic.com/translate/intl/ru/logo.png";
NSString *yourText = #"Some text here";
NSString *htmlString = [NSString stringWithFormat:#"<img src='%#'/><br><b>%#</b>", imageUrlString, yourText];
[myWebView loadHTMLString:htmlString baseURL:nil];

Related

Unable to read Images from xcode project in iPad IOS 10.2.1 64bit when download app from app store (testflight)

Unable to read Images from xcode project for example.
drag and drop one image to xcode project, then read using following code.
__weak NSString *strTemp = [NSString stringWithFormat:#"%#/s_%#",[[NSBundle mainBundle] resourcePath],[[self.arr_gallery objectAtIndex:i]valueForKey:#"filename_larger"]];
strTemp = [strTemp substringToIndex:[strTemp length]-3];
strTemp = [NSString stringWithFormat:#"%#png",strTemp];
// NSString *strTemp = [[NSString alloc] initWithFormat:#"%#/s_BGP001#2x.png",[[NSBundle mainBundle] resourcePath]];
UIImage *image;
image = [[UIImage alloc] initWithContentsOfFile:strTemp];
img.image = image;
strTemp = nil;
Write following code.
NSString *newStrTemp = [strTemp stringByReplacingOccurrencesOfString:#"jpg" withString:#"png"];
Instead of this one.
strTemp = [strTemp substringToIndex:[strTemp length]-3];
strTemp = [NSString stringWithFormat:#"%#png",strTemp];

Post image from local bundle to PinInterest iphone.

I have implemented the PinInterest through web view using this code:
NSString *description = #"Post your description here";
NSURL* sUrl = [NSString stringWithFormat:#"http://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/red_rose_flower3.jpg"];// pass your link here with your image name
// NSLog(#"URL:%#", sUrl);
NSString *protectedUrl = ( NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,( CFStringRef)sUrl, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
NSLog(#"Protected URL:%#", protectedUrl);
NSString *imageUrl = [NSString stringWithFormat:#"\"%#\"", sUrl];
imageUrl=[[NSBundle mainBundle]pathForResource:mAppDelegate.imagename ofType:nil];
NSString *buttonUrl = [NSString stringWithFormat:#"\"http://pinterest.com/pin/create/button/?url=www.flor.com&media=%#&description=%#\"", protectedUrl, description];
NSMutableString *htmlString = [[NSMutableString alloc] initWithCapacity:1000];
[htmlString appendFormat:#"<html> <body>"];
[htmlString appendFormat:#"<p align=\"center\"><img border=\"0\" src=\"http://assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></p>", buttonUrl];
[htmlString appendFormat:#"<p align=\"center\"><img width=\"300px\" height = \"400px\" src=%#></img></p>", imageUrl];
[htmlString appendFormat:#"<script type=\"text/javascript\" src=\"//assets.pinterest.com/js/pinit.js\"></script>"];
[htmlString appendFormat:#"</body> </html>"];
return htmlString;
And I have also tried using PinInterest iOS SDK, but all I found is that can only pin the image from url.
And I need to pin image from local bundle image. Any help will be greatly appreciated.
Read the document once, they said Right now, "we only support pinning an image from a url. In the future, we’ll add support for pinning local images." i think this answers your question. for more clarification reed this. hope this helps :)

iOS use ID3Lib to edit ID3 tags from mp3 files in the NSDocsDir

I am using ID3Lib example project to edit Title, Album and Artist ID3 tags on mp3 files and all is good until I come to adding an image (cover Art) if any one has any ideas how to finish off the below code that would be great:
- (void)demo {
nImage = [UIImage imageNamed:#"amazing-grace.jpg"];//unused
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"amazing-grace" ofType:#"jpg"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"amazing-grace-10s" ofType:#"mp3"];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:#"amazing-grace-10s.mp3"];
[[NSFileManager defaultManager] copyItemAtPath:path toPath:docPath error:nil];
// Read title tag
ID3_Tag tag;
tag.Link([path UTF8String]);
ID3_Frame *titleFrame = tag.Find(ID3FID_TITLE);
unicode_t const *value = titleFrame->GetField(ID3FN_TEXT)->GetRawUnicodeText();
NSString *title = [NSString stringWithCString:(char const *) value encoding:NSUnicodeStringEncoding];
NSLog(#"The title before is %#", title);
// Write title tag
tag.Link([docPath UTF8String]);
tag.Strip(ID3FID_TITLE);
tag.Strip(ID3FID_ALBUM);
tag.Strip(ID3FID_LEADARTIST);
tag.Clear();
ID3_Frame frame;
frame.SetID(ID3FID_TITLE);
frame.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);
NSString *newTitle = nTitle;
frame.GetField(ID3FN_TEXT)->Set((unicode_t *) [newTitle cStringUsingEncoding:NSUTF16StringEncoding]);
ID3_Frame frame2;
frame2.SetID(ID3FID_ALBUM);
frame2.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);
NSString *newAlbum = nAlbmum;
frame2.GetField(ID3FN_TEXT)->Set((unicode_t *) [newAlbum cStringUsingEncoding:NSUTF16StringEncoding]);
ID3_Frame frame3;
frame3.SetID(ID3FID_LEADARTIST);
frame3.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);
NSString *newArtist = nArtist;
frame3.GetField(ID3FN_TEXT)->Set((unicode_t *) [newArtist cStringUsingEncoding:NSUTF16StringEncoding]);
//this is the image code
ID3_Frame frame4;
frame4.SetID(ID3FID_PICTURE);
frame4.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);// dont think this should be TEXTENC
NSString *newImage = imagePath;
frame4.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF16StringEncoding]);//this line is also probably wrong
tag.AddFrame(frame);
tag.AddFrame(frame2);
tag.AddFrame(frame3);
tag.AddFrame(frame4);
tag.SetPadding(false);
tag.SetUnsync(false);
tag.Update(ID3TT_ID3V2);
NSLog(#"The title after is %# The album after is %# The artist after is %# The artist after is %#", newTitle,newAlbum,newArtist,newImage);
}
Use file path with UTF-8 encoding and will work fine:
frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]);
I get this code, From this source: https://android.googlesource.com/platform/external/id3lib/+/8a58cac9f8db5ed55b02b3ac24156d628af923d1/examples/test_pic.cpp
frame.SetID(ID3FID_PICTURE);
frame.GetField(ID3FN_MIMETYPE)->Set("image/jpeg");
frame.GetField(ID3FN_PICTURETYPE)->Set(11);
frame.GetField(ID3FN_DESCRIPTION)->Set("B/W picture of Saint-Saëns");
//frame.GetField(ID3FN_DATA)->FromFile("composer.jpg");
frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]);
tag.AddFrame(frame);
And! this work nice. :D

editing occurrences is not working

i am trying to get the path to a certain file
and then open it in a webview. so i need to replace each space by '%20'
NSString *test=#"filename";
NSString *finalPath12 = [test stringByAppendingString:#".pdf"];
NSString *path1 = [[NSBundle mainBundle] bundlePath];
NSString *finalPath1 = [path1 stringByAppendingPathComponent:finalPath12];
NSString *file =#"file://";
NSString *htmlfilename1 = [file stringByAppendingString:finalPath1];
NSString *pathtofile = [htmlfilename1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
any string is working except #"%20".
this works perfectly for exemple:
NSString *pathtofile = [htmlfilename1 stringByReplacingOccurrencesOfString:#" " withString:#"string"];
but i need the #"%20". What am i missing ? Thanks
There is already [NSString stringByAddingPercentEscapesUsingEncoding:] (reference) for that very purpose.
However in your case, as you want a URL, you can replace all the lines in your question with:
NSURL *url = [[NSBundle mainBundle] urlForResource:#"filename" withExtension:#"pdf"];
You need to use #"%%20".
As first % is treated as escape/wild character.
Or use
stringByAddingPercentEscapesUsingEncoding:

UIWebview - Loading PNG image - got error

I am trying to load the image(png) in UIWebview. But I got the following error
""ImageIO: PNGinvalid literal/lengths set"
I will get the "png" from the web service. I will store it in the following path. "Users/XXX/Library/Application Support/iPhone Simulator/4.3.2/Applications/E4DE3097-EA84-492F-A2A7-5882202F3B00/Documents/attachement.png "
When I reading the file from the path, I got the error. I am using the following code
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *commentHtml = [NSString stringWithFormat:#"<img src=\"%#\"/>", documentEntity.path];
NSString *html3 = [NSString stringWithFormat:#"<html><head/><body><div><b>COMMENTS:</b></div>%#</body></html>", commentHtml];
[self.documentView loadHTMLString:html3 baseURL:baseURL];
Please help me.
THanks
Girija
If you have saved the Image in Documents Folder, why are you fetching from MainBundle.
Fetching Image from the Documents Directory would be :
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:#"%#/attachement.png", aDocumentsDirectory];
UIImage *anImage = [UIImage imageWithContentsOfFile:aFilePath];

Resources