when i run this program on iphone after adding thre to four image memory warning come and app crash. so please help me Thanks i just get image reference and write on file and then getting image from file path. Thanks
- (IBAction)addPicsButtonClick:(id)sender
{
UIImagePickerController * picker = [[[UIImagePickerController alloc] init] autorelease];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSData *imageData = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 0.3f);
[imageData writeToFile:[NSString stringWithFormat:#"%#/%d.jpg", pathToDocuments, [app.images count]] atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
I ran the same code in a new app in the simulator, and had no crash problem or memory warning, and it created the files. I did take out [app.images count] and used a local variable instead, as app.images is not set anywhere in this code. So if that variable is trash or over-released, that might be your problem.
Related
im trying to make an app that has 4 ImageViews and I want to allow the user to change the background on the different ImageViews by pressing an UIButton below each ImageView. I have written a code for this from a tutorial showing to do this with just one ImageView. I just copied and pasted 4 times for each button and changed some variables. But when I run it only the first ImageView changes its picture even if I press UIButton for 2nd, 3rd or 4th ImageView. here the code:
#import "ViewController.h"
#interface ViewController ()
{
UIImagePickerController *imagePickerController;
UIImagePickerController *imagePickerController2;
UIImagePickerController *imagePickerController3;
UIImagePickerController *imagePickerController4;
}
#end
#implementation ViewController
#synthesize firstImageView, secondImageView, thirdImageView, fourthImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)firstChangeButton:(id)sender
{
imagePickerController = [[UIImagePickerController alloc]init];
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (IBAction)secondChangeButton:(id)sender
{
imagePickerController2 = [[UIImagePickerController alloc]init];
[imagePickerController2 setDelegate:self];
[imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController2 animated:YES completion:nil];
}
- (IBAction)thirdChangeButton:(id)sender
{
imagePickerController3 = [[UIImagePickerController alloc]init];
[imagePickerController3 setDelegate:self];
[imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController3 animated:YES completion:nil];
}
- (IBAction)fourthChangeButton:(id)sender
{
imagePickerController4 = [[UIImagePickerController alloc]init];
[imagePickerController4 setDelegate:self];
[imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController4 animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(image1);
NSString *myGrabbedImage = #"myGrabbedImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
[data writeToFile:fullPathToFile atomically:YES];
[[self firstImageView]setImage:image1];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController2:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info2
{
UIImage *image2 = [info2 objectForKey:UIImagePickerControllerOriginalImage];
NSData *data2 = UIImagePNGRepresentation(image2);
NSString *myGrabbedImage2 = #"myGrabbedImage2.png";
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory2 = [path2 objectAtIndex:0];
NSString *fullPathToFile2 = [documentDirectory2 stringByAppendingPathComponent:myGrabbedImage2];
[data2 writeToFile:fullPathToFile2 atomically:YES];
[[self secondImageView]setImage:image2];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController3:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info3
{
UIImage *image3 = [info3 objectForKey:UIImagePickerControllerOriginalImage];
NSData *data3 = UIImagePNGRepresentation(image3);
NSString *myGrabbedImage3 = #"myGrabbedImage3.png";
NSArray *path3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory3 = [path3 objectAtIndex:0];
NSString *fullPathToFile3 = [documentDirectory3 stringByAppendingPathComponent:myGrabbedImage3];
[data3 writeToFile:fullPathToFile3 atomically:YES];
[[self thirdImageView]setImage:image3];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController4:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info4
{
UIImage *image4 = [info4 objectForKey:UIImagePickerControllerOriginalImage];
NSData *data4 = UIImagePNGRepresentation(image4);
NSString *myGrabbedImage4 = #"myGrabbedImage4.png";
NSArray *path4 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory4 = [path4 objectAtIndex:0];
NSString *fullPathToFile4 = [documentDirectory4 stringByAppendingPathComponent:myGrabbedImage4];
[data4 writeToFile:fullPathToFile4 atomically:YES];
[[self fourthImageView]setImage:image4];
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
How should i apply it for all 4 ImageViews?
Thanks in advance!
The problem is that all image pickers will call the same delegate method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
When you set the delegate for the image picker controller like so:
[imagePickerController setDelegate:self];
You basically tell the image picker controller that self will be able to respond to the image picker delegate methods.
As you can see in the UIImagePickerControlDelegate, there's no such thing as:
– imagePickerController2:didFinishPickingMediaWithInfo:
and so on.
As a start, I would suggest rewriting your delegate method like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(image1);
NSString *myGrabbedImage = #"myGrabbedImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
[data writeToFile:fullPathToFile atomically:YES];
if (picker == imagePickerController) {
[[self firstImageView]setImage:image1];
} else if (picker == imagePickerController2) {
[[self secondImageView]setImage:image1];
} else if (picker == imagePickerController3) {
[[self thirdImageView]setImage:image1];
} else {
[[self fourthImageView]setImage:image1];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
This way, all image picker images will be handled by the same method, thus reducing the amount of code.
i want to pick a Video from Library. here is my code.
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
picker1.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker1.mediaTypes = mediaTypesAllowed;
[self presentModalViewController:picker1 animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
if ([picker isEqual:picker1])
{
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:#"public.movie"]){
NSLog(#"%#",info);
}
}
problem is when i select video from Library, simulator getting hanged. Even delegate method didFinishPickingMediaWithInfo not calling
Thanks in advance.
Try This
myImagePickerController.mediaTypes =
[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
or refer this
UIImagePickerControllerMediaType
Hope this helps u
I save picture from UIImagePicker this way:
Save picture in file and then I save path to the fail in NSUserDefaults and then in another class I retrieve the picture by this saved path.
Code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ideaImage.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker dismissModalViewControllerAnimated:YES];
}
-(void)saveIdea_alt
{
[self performSelector: #selector(saveIdea) withObject:nil afterDelay:0.1];
}
-(void)saveIdea
{
UIImage *ideaPhoto = ideaImage.image;
NSData *imageData = UIImagePNGRepresentation(ideaPhoto);
NSString* imageName = #"MyImage.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:NO];
NSArray *arrayKeys = [[NSArray alloc]initWithObjects:#"ideaName",#"ideaCost",#"ideaNote", #"ideaImage", nil];
NSArray *arrayObjects = [[NSArray alloc]initWithObjects:ideaName.text,ideaCost.text,ideaNote.text,fullPathToFile , nil];
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjects:arrayObjects forKeys:arrayKeys];
NSMutableArray *ideasArray = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:#"ideasArray"]];
[ideasArray addObject:dictionary];
[[NSUserDefaults standardUserDefaults]setObject:ideasArray forKey:#"ideasArray"];
[self dismissModalViewControllerAnimated:YES];
}
However, after that my app becomes slow and it saves and loads images slowly. What I do wrong ?
I don't really want to use Core Data because I already save all others properties of idea in NSUserDefaults (like cost, name, color and etc.)
After setting values in NSUserDefaults,Just write this line :
[[NSUserDefaults standardUserDefaults] synchronize];
Hope this will help.
Have you tried using
[self performSelectorInBackground: #selector(saveIdea) withObject:nil];
instead of
[self performSelector: #selector(saveIdea) withObject:nil afterDelay:0.1];
I'm using an imagePickerController to replace my existing images inside my view. The function works within the same IB-file. However I'd like to load the chosen image in another IB-File as well. I think that the solution is to give a name to the image when saved. After it is saved I'd like to call the image (by name) from my memory within my other IB-file.
Here's a snippit of code I'm using within the photopicker IB-file
-(IBAction)setPhoto{
image1.image = fotoView.image;
}
-(IBAction)getCameraPicture:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
fotoView.image = image;
NSData* imdata = UIImagePNGRepresentation ( image );
UIImage* im8 = [UIImage imageWithData:imdata];
UIImageWriteToSavedPhotosAlbum(im8, nil, nil, nil);
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
within my other class I'd like to call this image by means of:
if (#some condition){
UIImage *img = [UIImage imageNamed:#"Name of the image.png"];
[image1 setImage:img];
}
Help is greatly appreciated
Method for image picking and saving it to the directory:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/MyName.png",docDir];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];
}
Lateron you can call it by using:
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/MyName.png",docDir];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pngFilePath];
I have a UIPickerController to select videos in my App and it is crashing when selecting videos using the picker. When I feed the MPMoviePlayerController with the URL directly it works fine, but when the URL comes from the UIPickerController, it loads the movie correctly but crashes when the thumbnails are being created.
I have dumped the URL to the console, just after the file is selected and this is what I see
file://localhost/private/var/mobile/Applications/FA667F85-B009-46FA-A0B9-A7345A072651/tmp//trim.Ir0gqx.MOV
first question: why is the double bar before the file name?
second question: why the file name comes with a trim prefix if I have editing NO on the picker?
this is the picker code:
- (void) selectMovie:(id) sender {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
picker.delegate = self;
picker.allowsEditing = NO;
picker.videoQuality = UIImagePickerControllerQualityTypeHigh; // I've added this trying to make it stop compressing the video, but it won't... any clues?
UIPopoverController *aPopover =
[[UIPopoverController alloc] initWithContentViewController:picker];
aPopover.delegate = self;
CGRect myRect = [sender frame];
[aPopover presentPopoverFromRect:myRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ( [ mediaType isEqualToString:#"public.movie" ]){
movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
NSLog(#"%#", movieURL);
[self loadMovie:movieURL];
}
My thought is that your app does not have permission to generate thumbnails in the location where the movie is stored. Try copying the movie file out of the photo albums and into your documents directory:
NSFileManager *fm = [NSFileManager defaultManager];
[fm moveItemAtURL:fromURL toURL:toURL error:&error];
Assuming you have a toURL pointing to a filename in your docs directory.
I only know about the first one, because you need to feed to MPMoviePlayerController a url, a url has to start with a scheme, http://, smtp://, kind of that. file:// is also a scheme for file url