问题描述
我直奔主题,我的问题是,我有一个带有 3 个按钮和一个 uiimage 视图的 uiview 控制器.现在,
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,
当我按下第一个按钮时,我正在从 iphone 默认照片库中获取图像并放置在 uiimageview 上.
When i am pressing first button i am taking image from iphone default photo gallery and placing on uiimageview.
当我按下第二个按钮时,它将从照片库中获取另一张图片并放置在上一张图片上.
When i am pressing second button it will take another image from photo gallery and placing on previous image.
现在当我按下第三个按钮时,这两个图像将添加并使其成为一张图像并保存在相册中.
Now when i will press third button those two image will add and make it one image and save in the photo album.
我已经完成了第 1 步和第 2 步,但我该怎么做第 3 步??
i have done step 1 and step 2 but how can i do step 3??
编辑
假设图片中有一个没有胡须的人 (image1),但我要添加另一个胡须图片 (image2) 来制作一个看起来像胡须男人的图片 (image3).我想将图片 3 保存在相册中.
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.
提前感谢您的帮助..
推荐答案
我自己也有同样的问题,以前我是这样解决的
myself also having same issue previously i solved this way
试试这个类别代码....
try this category code....
.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
我的设计将是包含两个图像视图的视图(命名为组合视图),其中一个只是源图像[即没有胡须的人]在该图像顶部我有胡须图像...
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);
试试这个想法....
新更新:这个结构你需要维护,并且 theView
的背景颜色也是清晰的颜色
New Update:
this structure you need to maintain and also theView
's background color as clear color
+theView ---
|
->imageview1(person)
|
->imageView2(beard alone)
这篇关于如何在 iphone 应用程序开发中添加两个 uiimageview 并将其保存到 iphone 照片库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!