UIWebView shows preveously loaded page - ios

In my app a web view is shown inside each cell of a table view. To increase the response I we decided to save web page as images. So I made a web view which is not visible and loaded each page one by one and created the image of the page on
- (void)webViewDidFinishLoad:(UIWebView *)webView
The image is created, but even images are missing and odd images are created twice. That is if I am creating four images say image1, image2, image3, image4 . The first and second image are the same image2 will be missing then third and fourth are same and image4 will be missing.
Does anyone have any idea what's happening? How can I get all images?
EDIT: code for saving image
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
CGSize pageSize = webView.frame.size;
UIGraphicsBeginImageContext(pageSize);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
[[webView layer] renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
NSString * filName;
filName=[NSString stringWithFormat:#"%#_Potrait_%d.jpg",[currentSctionItem. titleName stringByReplacingOccurrencesOfString:#" " withString:#"_"],fileSavingAtIndex];
NSString * filePath = [documentsDir stringByAppendingPathComponent:filName];
[imageData writeToFile:filePath atomically:NO];
NSLog(#"File created At path:%#",filePath);
fileSavingAtIndex++;
if (fileSavingAtIndex>=currentSctionItem.numberOfPagesLandscape) {
//stop
}
else{
[webView loadHTMLString:[currentSctionItem potaraitHtmlAtThePage:fileSavingAtIndex] baseURL:baseURL];
}

I fixed it by creating a new web view for each page load. It is not the correct way but now it works
if (fileSavingAtIndex>=currentSctionItem.numberOfPagesLandscape) {
}
else{
UIWebView * webVw =[[UIWebView alloc] initWithFrame:PotraitwebViewFrame];
webVw.delegate=self;
[webVw loadHTMLString:[currentSctionItem potaraitHtmlAtThePage:fileSavingAtIndex] baseURL:baseURL];
}

Related

My app doesn't show image on iOS 7

I have an app,that contains UIImageView. it's working on iOS 8 but not showing image on iOS 7. I searched it, they always said that "it's auto layout problem." ok i removed all constraints on my view. but still not showing images.
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:sf._foto];
[Img setImage:[UIImage imageNamed:savedImagePath]];
Img.frame = CGRectMake(0,0,screenWidth,screenHeight);
Don't know if this will help but this is how I saved and then accessed an image through the documents directory
- (void)savePaystubToFile: (UIImage *)image {
if (image != nil)
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent: #"example.png" ];
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
- (UIImage*)loadPaystub {
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent: #"example.png" ];
// If nothing is stored at that location then return nil
UIImage * image = [UIImage imageWithContentsOfFile:path] ? [UIImage imageWithContentsOfFile:path] : nil;
return image;
}
The main difference here seems to be me using the imageWithContentsOfFile function and the basic set up of the process step by step.
I don't know whether this is the issues which you are struggling with but this is a better way of separating the storage and retrieval of your image. If this is working correctly then you know that the problem will be in accessing the frame.
If you are finding it is an autolayout issue, then you will need to remove the view from your xib and add it programmatically. I've had similar issues and just creating it programmatically, even if that means creating another xib with your view and loading it and adding it to your view's addSubview:. Every view that I want to modify later, but don't want to deal with modifying constraints, I add programmatically.
Alternatively, you could add your constraints and then put outlets to them in your code to modify when you want to change things.
Hopefully this helps.

Save a UIImage and reused it on UIImageview

I currently have the ability to take a photo or load from the camera and place an image in the UIImageView in my app be i want the ability to have a button that will save the image so when i reload the app the image is still in the UIImageView.
My though was a button to save the image once you have loaded it and in the
-(void)viewDidLoad
have the code to load the image that was saved but i don't know how to approach this.
Any help would be fantastic.
You can convert images to NSData using either UIImagePNGRepresentation or UIImageJPGRepresentation, then save the data to file calling one of NSData's writeToFile... methods.
Then when you restart your application, you can get the image by calling [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]
-- EDIT --
Save image
UIImage *image = ...;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
Load image
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
I used NSCachesDirectory since I'm assuming you don't want to have this image backed up (either through iCloud or iTunes). If you do then you'd want to use NSDocumentDirectory instead.

Loading from file not working.

I'm taking screenshots and writing them to file with this code:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [directories objectAtIndex:0];
NSString *key = [documentsDirectory stringByAppendingPathComponent:#"screenshots.archive"];
NSMutableArray *storageArray = [NSMutableArray arrayWithContentsOfFile:key];
if(!storageArray)
storageArray = [NSMutableArray arrayWithObject:data];
else
[storageArray addObject:data];
[storageArray writeToFile:key atomically:YES];
When i load the images again i do this:
pics = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];
ScreenshotViewController *scv = [[ScreenshotViewController alloc]init];
[[self navigationController]pushViewController:scv animated:YES];
NSInteger row = indexPath.row;
[scv setImage:[pics objectAtIndex:row]];
dataFilePath method:
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:#"screenshots.archive"];
}
The problem is that that the UIImageView, in the ScreenshotViewController remains blank. I have tested all kinds of things, and it is something about the way the images get saved/loaded into the array, but i'm not sure. I have also tested that the actual screenshot part is working by putting it into a UIImageView right away and adding it to the view.
NOTE:
If i change this
[scv setImage:[pics objectAtIndex:row]];
to this
[scv.myUIImageView setImage:[pics objectAtIndex:row]];
i get this exception: [__NSCFData _isResizable]: unrecognized selector sent to instance 0x847a280.
Whats the correct way of setting the image by?
Wow it got abit messy, hope its readable.
At first glance, and assuming your read/write code is correct, you are saving the image with PNG representation.
Thus, when reading, you will need to create a UIImage like this:
UIImage * myPic = [UIImage imageWithData:[pics objectAtIndex:row]];
Then try showing that image

Resizing an image from UIImagePickerController not working

I am following this post on how to resize an image. I placed the + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; in selectExerciseImageViewController.h and copied the relevant code to selectExerciseImageViewController.m.
Then I attempt to resize the image, save it, and retrieve the saved image file into a UIImageView. but for some reason the image is never showing the the UIImageView and the last NSLog returns null
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage* newImage = [selectExerciseImageViewController imageWithImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]
scaledToSizeWithSameAspectRatio:CGSizeMake(40.0,40.0)];
NSData* imageData = UIImagePNGRepresentation(newImage);
// Give a name to the file
NSString* imageName = #"MyImage.png";
// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
//imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
_testimg.image = [UIImage imageNamed:#"MyImage.png"];
NSLog(#"asd %#",[UIImage imageNamed:#"MyImage.png"]);
}
[UIImage imageNamed:#"MyImage.png"];
This loads an image from the main application bundle. You are writing the file to the documents directory. You need to instantiate the UIImage object with a different method. Try:
[UIImage imageWithContentsOfFile:fullPathToFile];

Is possible to make screen shot in ipad programmatically? [duplicate]

how to save the screenshot programmatically in Ipad with image name taken from the user?
i've found a snippet which works but doesn't asks for the name of the image.
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
Also is it possible to create multiple folders in photo albums and save the image in it programmatically??
You cannot specify a name when you save the image to the photo album. If you want to do that, you have to save the image to the app's directory folder and then build a way to show to the user, the images he has saved internally and allow him to delete, load, etc.
This is how to save a PNG image to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:myFileName];
NSData *imageData = UIImagePNGRepresentation(myImage);
[imageData writeToFile:appFile atomically:YES];
myImage is your UIImage and
myFileName is the name you want

Resources