Taking a picture from the camera and show it in a UIImageView

后端 未结 2 416
暖寄归人
暖寄归人 2021-01-25 13:15

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 properl

相关标签:
2条回答
  • 2021-01-25 14:04

    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();
    
    0 讨论(0)
  • 2021-01-25 14:12

    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.

    0 讨论(0)
提交回复
热议问题