Cropping an Image to the shape of an Overlay - iOS

前端 未结 3 556
悲哀的现实
悲哀的现实 2021-02-14 17:27

I\'m adding the overlay using pickerController\'s view and the uinavigationcontrollerdelegate as below.

-(void)navigationController:(UINavigationController *)nav         


        
3条回答
  •  感情败类
    2021-02-14 17:34

    Found this blog post entry very interesting, neat and simple to follow by Nimit Parekh.

    Following code is copy/paste into your “viewcontroller.h” file:

    #import <UIKit/UIKit.h>
    
    @interface UIImagePickerDemoViewController : UIViewController< UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    
    @property(nonatomic,retain) UIImagePickerController *imgPicker;
    @property(nonatomic,retain) IBOutlet UIImageView *image_view;
    
    //- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect;
    - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage;
    @end
    

    Following Code copy/paste into “viewcontroller.m” file:

    // Following method is use for the mask the image.
    
    - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    
        CGImageRef maskRef = maskImage.CGImage; 
    
        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                            CGImageGetHeight(maskRef),
                                            CGImageGetBitsPerComponent(maskRef),
                                            CGImageGetBitsPerPixel(maskRef),
                                            CGImageGetBytesPerRow(maskRef),
                                            CGImageGetDataProvider(maskRef), NULL, false);
    
        CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
        return [UIImage imageWithCGImage:masked];
    }
    
    // Following method is use for Cropping the image for a perticular size.
    
    - (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
        CGContextTranslateCTM(currentContext, 0.0, rect.size.height);
        CGContextScaleCTM(currentContext, 1.0, -1.0);   
    
        CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
        CGContextClipToRect( currentContext, clippedRect);
        CGRect drawRect = CGRectMake(rect.origin.x * -1,rect.origin.y * -1,imageToCrop.size.width,imageToCrop.size.height);
        CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
        CGContextScaleCTM(currentContext, 1.0, -1.0);
    
        UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return cropped;
    }
    
    // Calling the method of maskimage.
    
    //=============================Camera Enable(display)============================================
    -(IBAction)next:(id)sender{
    
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        [self presentModalViewController:self.imgPicker animated:YES];
    }
    
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [picker dismissModalViewControllerAnimated:YES];
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        [picker dismissModalViewControllerAnimated:YES];
        UIImage *img =  [info objectForKey:@"UIImagePickerControllerOriginalImage"];
            self.image_view.image=[self maskImage:img withMask:[UIImage imageNamed:@"frame.png"]];
    }
    //===============================================================================================
    
    // Calling the method of cropping the image.
    
    //=============================Camera Enable(display)============================================
    -(IBAction)next:(id)sender{
    
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        [self presentModalViewController:self.imgPicker animated:YES];
    }
    
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [picker dismissModalViewControllerAnimated:YES];
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        [picker dismissModalViewControllerAnimated:YES];
        UIImage *img =  [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        self.image_view.image = [self imageByCropping:img toRect:CGRectMake(0, 0, 420, 40)];
    }
    //===============================================================================================
    

    Output:

    iPhone output

    Grab the source code here.

提交回复
热议问题