I am rookie to iOS. In my project i need to add thumbnail image of web pages in image view.I created a NSObject class for that. I initiated web view in that class but it won't call the UIWebView Delegates.I posted the code here. If any one have solution or the library file for thumbnail processing that might be useful.Thank you.
#interface ThumbNail()<UIWebViewDelegate>
{
UIImageView *thumbNailImageView;
UIWebView *mockUpWebView;
UIImage *viewImage;
}
#end
#implementation ThumbNail
-(void)webViewDidFinishLoad:(UIWebView *)webView {
thumbNailImageView.image = [self captureScreen:webView];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(#"Errorrr.............");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Got it");
return YES;
}
-(UIImage*)captureScreen:(UIWebView*) viewToCapture{
CGSize overallSize = CGSizeMake(mockUpWebView.frame.size.width, mockUpWebView.frame.size.width);
UIGraphicsBeginImageContext(viewToCapture.scrollView.contentSize);
viewToCapture.bounds = CGRectMake(0, 0, overallSize.width, overallSize.height);
while (viewToCapture.loading) {
[NSThread sleepForTimeInterval:0.1];
}
[viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
-(void)getThumbNailForImageView:(UIImageView *)imageView{
thumbNailImageView = imageView;
NSString *urlAddress = #"https://in.yahoo.com/?p=us";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
CGRect bounds = [[UIScreen mainScreen] bounds];
mockUpWebView = [[UIWebView alloc]initWithFrame:bounds];
NSLog(#"bounds = %#",NSStringFromCGRect(bounds));
mockUpWebView.delegate = self;
[mockUpWebView loadRequest:requestObj];
}
#end
I am trying to get thumbnail image by calling getThumbNailForImageView.
Related
i am loading url in webview.
i have two types of data to display
1)text(html string)
2)url
here is my code
To display text
[self.webView loadHTMLString:[self.arr objectAtIndex:indexPath.row][#"text"] baseURL:nil];
To display url
NSString *str = [NSString stringWithFormat:#"%#%#",URL,strpdf];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
issue is when i loading both data,data is overwrite.
i want to display both data in webview
but right now only text or image is displaying.
what is solution for this.
My demo is below:
#import "ViewController.h"
#interface ViewController ()<UIWebViewDelegate>
#property(nonatomic, strong) UIWebView *webView;
#property (nonatomic, copy) NSString *html_str;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI ];
}
- (void)initUI {
_html_str = #"I love google..."; // the html text you want
CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
_webView = [[UIWebView alloc] initWithFrame:frame];
_webView.delegate = self;
NSString *str = [NSString stringWithFormat:#"http://chsezhsan195030.e-fae.cn/"];
NSURL *websiteUrl = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webView loadRequest:urlRequest];
[self.view addSubview:self.webView];
}
#pragma mark - webview delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script_str = [NSString stringWithFormat:#"var body = document.getElementsByTagName('body')[0];\
var script = document.createElement('div');\
script.innerText = '12345678'%#;\
script.style.width = window.innerWidth + 'px';\
script.style.height = '300px';\
script.style.backgroundColor = '#eeeeee';\
body.appendChild(script); \
body.insertBefore(script, body.childNodes[0]);",_html_str];
NSString *function = [NSString stringWithFormat: #"var script = document.createElement('script');\
script.type = 'text/javascript';\
script.text = \'function myFunction() { \
%# };'\
document.getElementsByTagName('head')[0].appendChild(script);", script_str];
[self.webView stringByEvaluatingJavaScriptFromString:function];
[self.webView stringByEvaluatingJavaScriptFromString:#"myFunction()"];
}
#end
But you should attention most of the url is prevented for inject js code,especially the https url, so you can not insert the html text above the google's index page.
I want to use AsyncImageView to load and image from a URL and then add this image to a UIView. I have retrieved my URL but when I try to use the method loadImageWithURL I can't because it is not declared in the interface for AsyncImageView but rather for AsyncImageLoader. Should I be using the interface AsyncImageLoader instead? Can anybody help me figure out how to modify my code to do this?
Link to image with code:
http://imgur.com/QPv32td
- (void)viewDidLoad {
CGRect frame = image.frame;
AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:frame];
asyncImage.tag = 999;
AppDelegate *thisDelegate = [[UIApplication sharedApplication] delegate];
if (thisDelegate.isEchoesUser == YES) {
url = [NSURL URLWithString:[[NSString stringWithFormat:#"%#%#", ECHOES_REST_SERVICE_THUMB3,
[imageURL stringByReplacingOccurrencesOfString:ECHOES_REST_ADDRESS withString:#""]]
stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
}else if (thisDelegate.isEchoesUser == NO){
url = [NSURL URLWithString:[[NSString stringWithFormat:#"%#%#", REST_SERVICE_THUMB3,
[imageURL stringByReplacingOccurrencesOfString:REST_ADDRESS withString:#""]]
stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
}
[asyncImage loadImageWithURL:url];
[image addSubview:asyncImage];
// Set the caption;
[caption setText:captionText];
[super viewDidLoad];
}
Use a AsyncImageLoader instead, AsyncImageView doesn't have this function
I have a Gif in a UIWebView, but when the animation ends, it stops.
How I can do to never stop?
This is my code:
self.video is my UiWebView
my gif is videoGif.gif
- (void)viewDidLoad
{
[super viewDidLoad];
self.video.delegate=self;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"videoGif" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
[self.video loadData:gif MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
self.video.userInteractionEnabled = NO;
self.video.scalesPageToFit=YES;
self.video.contentMode = UIViewContentModeScaleAspectFit;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
self.video.scalesPageToFit=YES;
self.video.contentMode = UIViewContentModeScaleAspectFit;
}
Thanks!!
You can you use UIView Animations. Here i use UIButton for animation. It may be and alternative way. Here my code:
NSMutableArray *imageArray=[NSMutableArray new];
for(int i=1;i<=16;i++){
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"win_%d.png",i]]];
}
[animatedButton setImage:[UIImage imageNamed:#"win_1.png"] forState:UIControlStateNormal];
[animatedButton.imageView setAnimationImages:[imageArray copy]];
animatedButton.imageView.animationDuration = .50;
[animatedButton.imageView startAnimating];
I am using the following code example to add Instagram photo sharing to my iphone app:
http://sourceforge.net/projects/nimitparekh/files/Open%20instagram/OpenInstagramController.zip/download
I am having the same problem with the code example and also with my app. The "Open In" menu appears with Instagram, iPhoto, etc, but none of the apps open when pushed. Could one of you geniuses take a peek at the code example and tell me what I missing? Thanks!
Here is the .h code:
#interface ViewController : UIViewController<UIDocumentInteractionControllerDelegate>{
IBOutlet UIImageView *imageView;
}
#property(nonatomic,retain)UIDocumentInteractionController *docFile;
#property (nonatomic, retain) UIDocumentInteractionController *dic;
-(IBAction)saveToInstagram:(id)sender;
And here in the .m code:
#synthesize docFile = _docFile;
-(IBAction)saveToInstagram:(id)sender {
CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/test.ig"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
self.docFile.UTI = #"com.instagram.photo";
self.docFile = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.docFile=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://media?id=MEDIA_ID"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
}
else {
NSLog(#"No Instagram Found");
}
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
Thanks.
I'm not sure about how you construct your file NSURL instance, but one thing that's clear from your code is that you don't actually save the image you rendered.
Before calling UIGraphicsEndImageContext() you need to do something like
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
and then something like
NSData * imageData = UIImageJPEGRepresentation(image, 0.75);
[imageData writeToURL: igImageHookFile atomically:YES];
I am attempting to use this method I've constructed to save the layer of a UIWebView as an image. That is working but the SVG I have loaded into the UIView is not getting rendered in the image, only the background of the UIWebView.
+ (BOOL)imageFromSVGinWebView:(UIView *)theView usePNG:(BOOL)usePNG; {
UIGraphicsBeginImageContext(theView.frame.size);
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *layerIMGData = nil;
NSString *pathComponent = nil;
if (usePNG) {
layerIMGData = UIImagePNGRepresentation(layerImage);
pathComponent = #"png";
}else {
layerIMGData = UIImageJPEGRepresentation(layerImage,1.0f);
pathComponent = #"jpg";
}
NSString *appFile = [[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:String(#"img.%#",pathComponent)];
NSError *error = nil;
BOOL returnBOOL = [layerIMGData writeToFile:appFile options:NSDataWritingAtomic error:&error];
if(error != nil)
ILogPlus(#"%#",error);
return returnBOOL;
}
In summary, I've loaded an SVG into a UIWebView, saved that UIWebView as an image, opened the image and the image does not have the SVG in it. I have done this with other views successfully, something is different about the SVG or UIWebView.
UPDATE:
If I add a delay to make the app wait a few seconds to execute the image saving code then it works. Putting that code in webViewDidFinishLoad does NOT work. Obviously webViewDidFinishLoad doesn't count SVGs.
I have found that if you add a .1 second delay it will work
- (void)webViewDidFinishLoad:(UIWebView *)webView; {
[self performSelector:#selector(postLoadActions:) withObject:webView afterDelay:0.1f];
}
- (void)postLoadActions:(UIWebView *)webView; {
if (![CCConversion imageFromSVGinWebView:webView usePNG:YES]) {
ILogPlus(#"Didn't work");
}
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[CCFileManagement conversionsDirectory] error:NULL];
ILogPlus(#"%#",array);
//[webView removeFromSuperview];
[webView release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[CCFileManagement conversionsDirectory] stringByAppendingPathComponent:#"img.png"]]];
imageView.frame = CGRectMake(90, 90, imageView.frame.size.width, imageView.frame.size.height);
imageView.backgroundColor = [UIColor clearColor];
[self.view performSelector:#selector(addSubview:) withObject:imageView afterDelay:2.0f];
[imageView release];
}