How can i add two uiimageview and save it to iphone photo gallery in iphone application development?

后端 未结 2 1143
忘掉有多难
忘掉有多难 2021-01-15 01:46

I am going straight to the point,my problem is, I have a uiview controller which is taking 3 buttons and a uiimage view.Now,

  1. When i am pressing first button

2条回答
  •  滥情空心
    2021-01-15 02:23

    myself also having same issue previously i solved this way

    try this category code....

    .h file:

    #import 
    
    @interface UIImage (MyImage)
    
    
    + (UIImage*)imageFromView:(UIView*)view;
    + (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize;
    + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
    + (void)beginImageContextWithSize:(CGSize)size;
    + (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds;
    
    //masking the image ....
    
    + (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage;
    
    @end
    

    .m file:

    #import "UIImage+MyImage.h"
    
    @implementation UIImage (MyImage)
    
    + (void)beginImageContextWithSize:(CGSize)size
    {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            if ([[UIScreen mainScreen] scale] == 2.0) {
                UIGraphicsBeginImageContextWithOptions(size, YES, 2.0);
            } else {
                UIGraphicsBeginImageContext(size);
            }
        } else {
            UIGraphicsBeginImageContext(size);
        }
    }
    
    + (void)endImageContext
    {
        UIGraphicsEndImageContext();
    }
    
    + (UIImage*)imageFromView:(UIView*)view
    {
        [self beginImageContextWithSize:[view bounds].size];
        BOOL hidden = [view isHidden];
        [view setHidden:NO];
        [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        [self endImageContext];
        [view setHidden:hidden];
        return image;
    }
    
    + (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
    {
        UIImage *image = [self imageFromView:view];
        if ([view bounds].size.width != newSize.width ||
            [view bounds].size.height != newSize.height) {
            image = [self imageWithImage:image scaledToSize:newSize];
        }
        return image;
    }
    
    + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
    {
        [self beginImageContextWithSize:newSize];
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        [self endImageContext];
        return newImage;
    }
    
    
    + (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds {
        CGImageRef imageRef = CGImageCreateWithImageInRect(myImage.CGImage, bounds);
        UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        CGSize asd =  croppedImage.size;
        return croppedImage;
    }
    
    
    
    
    + (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);
    
        CGImageRelease(mask);
        CGImageRelease(maskRef);
        return [UIImage imageWithCGImage:masked];
    
    }
    
    
    
    @end
    

    my design will be view(named as combinedView) that contains two image view one is just the source image[i.e man without beard] on the top of that image i am having beard image...

    then i make the image by this way....

    UIImage *myMaskedImage=[UIImage imageFromView:combinedView ];
    

    for saving this to album use this...

     UIImageWriteToSavedPhotosAlbum(myMaskedImage, self, nil, nil);
    

    try this idea....

    New Update: this structure you need to maintain and also theView's background color as clear color

                  +theView ---
                     |
                     ->imageview1(person)
                     |
                     ->imageView2(beard alone)
    

提交回复
热议问题