I’m writing an application that gives the user the option to resize an image that they take and upload it to a service. The resizing seems to work fine, but it’s not storing the re-created image in the proper place, because the application doesn’t seem to think that there’s a photo loaded.
Note that the first function in the if statement (regarding “normal_size”) works fine. I’ve tried to re-create those two lines in the additional size parameters, but I can’t seem to figure out why it’s not storing it. This is driving me crazy. Here’s the code:
// Set image dictionaries
iInfoDict = [[NSDictionary alloc] initWithDictionary:info];
NSLog(@"infor ===== %@", info);
UIImage* image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSLog(@"iImage name === %@", iImage);
// Determines path of image taken / chosen
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
CGSize size;
// Not sure what this does other than dismissing the pickerViewController
NSData* data = UIImageJPEGRepresentation(image, 0.9);
[data writeToFile:appFile options:nil error:nil];
NSLog(@"Documents ==== %@", documentsDirectory);
[picker dismissModalViewControllerAnimated:YES];
// photo.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// IMAGE RESIZE CODE
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"normal_image"])
{
[photoData setObject:[iInfoDict objectForKey:@"UIImagePickerControllerOriginalImage"] forKey:@"image"];
photo.image = [iInfoDict objectForKey:@"UIImagePickerControllerOriginalImage"];
}
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"small_image"])
{
CGSize size = CGSizeMake (600, 450);
UIImage *origImage = [iInfoDict objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *destImage = [origImage resizedImage:size interpolationQuality:kCGInterpolationHigh];
NSLog(@"Image Size: %@", NSStringFromCGSize(destImage.size));
[photoData setObject:destImage forKey:@"image"];
photo.image = destImage;
}
Either I’m not understanding your question, or you don’t understand what you’re doing. I suspect the latter, based on the comment
// Not sure what this does other than dismissing the pickerViewController, which is promptly followed by two lines that write the unresized image to the documents directory.You probably want to resize the image before you write it out, so I suggest moving the writing to the end, after you’ve resized.
Delete the two lines:
And, at the end, insert these two lines (no guarantee, written from memory):