Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Objective-C: Image manipulation and processing

I am developing an iOS application (Objective C) in which I needed to deal with image manipulation.

Functions to do in module of application are,

Take any image (parent-image) that you want to edit. Then put other images like similes, emojies, sticker into that image. The sub-images should be Scalable, Rotatable, Draggable within parent image bounds.

I succeeded to manage sub-images scale, rotate, drag effects.

But the problem is, during the scaling, rotating, dragging sub-image, the portion of image that lies outside the parent image should become blur or lower its alpha value than the portion of image inside the parent-image.

I’ve attached the reference screenshot of the output that I am looking for.

This is the reference image of the output, I am looking for

Edit: Here is the code snippet.

UIImageView* randomView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 100, 100)];
        randomView.image = image; //image coming from gallery
        randomView.userInteractionEnabled = YES;
        randomView.alpha = 0.8;

        imageView.backgroundColor = [UIColor clearColor];
        imageView.opaque = NO;

        randomView.backgroundColor = [UIColor clearColor];
        randomView.opaque = NO;

        [self addPanGesture:randomView];
        [self addPinchGesture:randomView];
        [self addRotateGesture:randomView];

        [imageView addSubview:randomView];
like image 500
Rashesh Bosamiya Avatar asked Oct 30 '22 00:10

Rashesh Bosamiya


1 Answers

I think this demo you are asking for... Check this demo :

http://sugartin.info/2011/10/21/mask-and-crop-of-an-image/

First crop the image by another image shape & then Replace the following Function in the demo to get your requirement like this....

- (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"]];
    UIImageView *viewImg=[[UIImageView alloc]initWithFrame:self.image_view.frame];
    viewImg.image=img;
    viewImg.alpha=0.4;
    [self.view addSubview:viewImg];
    [self.view sendSubviewToBack:viewImg];

}

This demo gives me this type of result...

enter image description here

In case, if you want only masking image function with another Image, Here it is..

- (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];

}

Hope this would help.. Let me know if that works or not...Thanks.. :)

like image 188
Sneha Avatar answered Nov 14 '22 23:11

Sneha