1

I have a view with some fields (name, price, category) and a segmented control, plus this button to take picture.
If I try this on the simulator (no camera) it works properly: I can select the image from the camera roll, edit it and go back to the view, which will show all the fields with their contents .
But on my iphone, when I select the image after the editing and go back to the view, all the fields are empty exept for the UIImageView.
I also tried to save the content of the fields in variables and put them back in the "viewWillApper" method, but the app crashes.
Start to thinking that maybe there is something wrong methods below

EDIT

I found the solution here. I defined a new method to the UIImage class. (follow the link for more information).
Then I worked on the frame of the UIImageView to adapt itself to the new dimension, in landscape or portrait.

-(IBAction)takePhoto:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imgPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    } else { 
        imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    [self presentModalViewController:self.imgPicker animated:YES];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];

    NSDate *date = [NSDate date];
    NSString *photoName = [dateFormatter stringFromDate:date];    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUs    erDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", photoName]];

    UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];

    // ---------RESIZE CODE--------- //
    if (picture.size.width == 1936) {
        picture = [picture scaleToSize:CGSizeMake(480.0f, 720.0f)];
    } else {
        picture = [picture scaleToSize:CGSizeMake(720.0f, 480.0f)];
    }
    // --------END RESIZE CODE-------- //

    photoPreview.image =  picture;

    // ---------FRAME CODE--------- //
    photoPreview.contentMode = UIViewContentModeScaleAspectFit;
    CGRect frame = photoPreview.frame;
    if (picture.size.width == 480) {
        frame.size.width = 111.3;
        frame.size.height =167;
    } else {
        frame.size.width = 167;
        frame.size.height =111.3;
    }
    photoPreview.frame = frame;
    // --------END FRAME CODE-------- //


    NSData *webData = UIImagePNGRepresentation(picture);
    CGImageRelease([picture CGImage]);
    [webData writeToFile:imagePath atomically:YES];
    imgPicker = nil;
}

Now I have a new issue! If I take a picture in landscape, and try to take another one in portrait, the app crashs. Do I have to release something?

Oiproks
  • 764
  • 1
  • 9
  • 34

2 Answers2

3

I had the same issue, there is no edited image when using the camera, you must use the original image :

originalimage = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];

if ([editingInfo objectForKey:UIImagePickerControllerMediaMetadata]) {
    // test to chek that the camera was used
    // especially I fund out htat you then have to rotate the photo
    ...

If it was cropped when usign the album you have to re-crop it of course :

if ([editingInfo objectForKey:UIImagePickerControllerCropRect] != nil) {
    CGRect cropRect = [[editingInfo objectForKey:UIImagePickerControllerCropRect] CGRectValue];
    CGImageRef imageRef = CGImageCreateWithImageInRect([originalimage CGImage], cropRect);
    chosenimage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
} else {
    chosenimage = originalimage;
}

The croprect info is also present for the camera mode, you need to check how you want it to behave.

gregory
  • 826
  • 1
  • 6
  • 6
  • Thanks a lot. Now I have the image. But, really, i can't use a 3.1 MB photo... there is no way to use the edited? Or compress the original? – Oiproks Oct 19 '11 at 12:47
  • I do : UIImage* compressedimage = [UIImage imageWithData:UIImageJPEGRepresentation(chosenimage, 0.0)]; to compress the image. The 0.0 is the compression ratio to be tuned, this lead to about 100 kb per image – gregory Oct 19 '11 at 13:59
  • Also with 0.0 every image is more than 2MB.
    There is no way to use the "editedImage" or "cropRect" of the controller?
    – Oiproks Oct 19 '11 at 18:35
  • None that I know for editedImage, you can use the cropRect but be careful it has values even when you've not cropped yourself. As for the size I re-read my code and noticed I apply the UIImageJPEG... method twice, worth a try – gregory Oct 19 '11 at 20:16
  • None that I know for editedImage, you can use the cropRect but be careful it has values even when you've not cropped yourself. As for the size I re-read my code and noticed I apply the UIImageJPEG... method twice, worth a try – gregory Oct 19 '11 at 20:16
  • I solved in another way... I added a method to UIImage to resize the images. Maybe is too much, but it's unbelievable that objective-c doen't have a method like this. I'll edit my question with my solution. Thanks a lot for you're help. You gave me some good ideas. – Oiproks Oct 19 '11 at 20:40
0

To Crop image i think this may help you

UIImage *croppedImage = [self imageByCropping:photo.image toRect:tempview.frame];

CGSize size = CGSizeMake(croppedImage.size.height, croppedImage.size.width);
UIGraphicsBeginImageContext(size);

CGPoint pointImg1 = CGPointMake(0,0);
[croppedImage drawAtPoint:pointImg1 ];

[[UIImage imageNamed:appDelegete.strImage] drawInRect:CGRectMake(0,532, 150,80) ];

UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
croppedImage = result;

UIImageView *mainImageView = [[UIImageView alloc] initWithImage:croppedImage];

CGRect clippedRect = CGRectMake(0, 0, croppedImage.size.width,  croppedImage.size.height);

CGFloat scaleFactor = 0.5;

UIGraphicsBeginImageContext(CGSizeMake(croppedImage.size.width * scaleFactor, croppedImage.size.height * scaleFactor));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextClipToRect(currentContext, clippedRect);   

//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of code
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);

[mainImageView.layer renderInContext:currentContext];

appDelegete.appphoto = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
Nilesh Kikani
  • 2,628
  • 21
  • 37