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

雨燕双飞 提交于 2019-12-30 11:30:28

问题


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 i am taking image from iphone default photo gallery and placing on uiimageview.

  2. When i am pressing second button it will take another image from photo gallery and placing on previous image.

  3. Now when i will press third button those two image will add and make it one image and save in the photo album.

i have done step 1 and step 2 but how can i do step 3??

EDIT

suppose a man without beard in a picture(image1) but i am adding another beard image(image2) to make an image where he will look like beard man(image3). i want to save image 3 in photo album.

Thanks In Advance for any help..


回答1:


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)



回答2:


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);


来源:https://stackoverflow.com/questions/12453913/how-can-i-add-two-uiimageview-and-save-it-to-iphone-photo-gallery-in-iphone-appl

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