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

别说谁变了你拦得住时间么 提交于 2019-12-01 11:04:51

myself also having same issue previously i solved this way

try this category code....

.h file:

#import <UIKit/UIKit.h>

@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)
Romit M.

In Button TouchUpInSide Event Write thid code

Create first image

UIImage *myImage = [UIImage imageNamed:@"image1.png"];

save first image

UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil);

Create second image

myImage = [UIImage imageNamed:@"image2.png"];

Save second image

UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!