Resizing an image from UIImagePickerController not working - ios

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];

Related

Load image previously saved from the camera roll

So I have an image saved from the camera roll or camera and have a saved the directory and image name to reference back to. When I go back to where I want that image to show it won't. It's just a blank image view.
I seem like it should be straight forward and I have done my own research, but no answer seems to be working for me.
Below is the image path and the code I am using to try and show the image. What am I doing wrong here?
/Users/*****/Library/Developer/CoreSimulator/Devices/FA19C634-C029-4518-BAE1-E7CC601FB9C2/data/Containers/Data/Application/72B5C82B-A8FB-451D-A6DC-DF2C3F3ED12D/Documents/Profile Pics/CYKGXryEDj-1.jpg
self.imageView.image = [UIImage imageWithContentsOfFile:profileImg];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"]; //Add the file name
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];
I did only one time using AVFoundation framework -
I was saving images as data and showing them by using imageWithData method. hope this will help you.

how to load image to uiimageview

how to load image to uiimageview from a custom folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString*ext= [NSString stringWithFormat:#"/Imagenes/30775.png"];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:ext];
//option 1
UIImage * myImage = [UIImage imageWithContentsOfFile: dataPath];
_img.image = myImage
///option 2
UIImageView * myImageView = [[UIImageView alloc] initWithImage: myImage];
_img = myImageView;
///option 3
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dataPath]];
_img.image = [UIImage imageWithData:imgData];
i try 3 options but not working
any idea ?
the image is loading from a custom folder
i checked the path and is correct but not load
Your path #"/Imagenes/30775.png" seems to be wrong. Double check it. Otherwise following should work
UIImage * myImage = [UIImage imageWithContentsOfFile: dataPath];
_img.image = myImage
NSString*ext= [NSString stringWithFormat:#"/Imagenes/30775.png"];
Your path is wrong.I guess the Imagenes is a group in your project. So xcode is not create a folder in your project main folder. The groups is only helps you to view the files easily in the xcode program.
Also you don't need stringWithFormat.
Try this;
NSString*ext= #"30775.png";

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.

UIWebView shows preveously loaded page

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];
}

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