我直截了当地说,我的问题是,我有一个uiview控制器,它带有3个按钮和一个uiimage视图。现在,
当我按下第一个按钮时,我正在从iphone默认照片库中拍摄图像并放置在uiimageview上。
当我按下第二个按钮时,它会从照片库中拍摄另一张图像并放在上一张图像上。
现在,当我按第三个按钮时,这两个图像将添加并制作一个图像并保存在相册中。
我已完成第1步和第2步,但我该怎么做第3步?
修改
假设一个男人在照片中没有胡子(图像1),但我正在添加另一个胡须图像(图像2)来制作一个他看起来像胡子男人的图像(图像3)。我想在相册中保存图像3。
在此先感谢您的帮助..
答案 0 :(得分:1)
按钮 TouchUpInSide 事件 写下你的代码
制作第一张图片
UIImage *myImage = [UIImage imageNamed:@"image1.png"];
保存第一张图片
UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil);
制作第二张图片
myImage = [UIImage imageNamed:@"image2.png"];
保存第二张图片
UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil);
答案 1 :(得分:1)
尝试此类别代码....
.h文件:
#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文件:
#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
我的设计将是视图(命名为combinedView),其中包含两个图像视图,一个只是源图像[即没有胡子的男人]在该图像的顶部我有胡须图像...
然后我通过这种方式制作图像......
UIImage *myMaskedImage=[UIImage imageFromView:combinedView ];
将此保存到相册使用此...
UIImageWriteToSavedPhotosAlbum(myMaskedImage, self, nil, nil);
尝试这个想法......
新更新:
您需要维护此结构,并将theView
的背景颜色视为清晰颜色
+theView ---
|
->imageview1(person)
|
->imageView2(beard alone)