我需要在一个视图中显示两个图像。到目前为止,我只显示了一个图像,但是如何在一个视图中显示两个图像?在 iPhone 上可以吗?如果是这样,你能指导我怎么做吗?
注意:我需要同时显示两个图像。
我需要在一个视图中显示两个图像。到目前为止,我只显示了一个图像,但是如何在一个视图中显示两个图像?在 iPhone 上可以吗?如果是这样,你能指导我怎么做吗?
注意:我需要同时显示两个图像。
如果要重叠 2 个图像,请粘贴此代码,
UIImage *a = [ your 1st iamge];
UIImage *b = [ your 2nd iamge];
UIGraphicsBeginImageContext(a.size);
[a drawInRect:CGRectMake(0, 0, a.size.width, a.size.height)];
[b drawInRect:CGRectMake(a.size.width - b.size.width, a.size.height - b.size.height, b.size.width, b.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[YourImageView setImage:result];
UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 110, 100, 50)];
imgView1.image = [UIImage imageNamed:@"image1.png"];
imgView2.image = [UIImage imageNamed:@"image2.png"];
[self.view addSubview:imgView1];
[self.view addSubview:imgView2];
您可以使用 - 2 个实例UIImageView
,并根据需要对它们进行布局;- 或子类化并通过覆盖UIView
绘制 2秒UIImage
drawInRect:
- (void)displayImages
{
UIImageView *firstImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage"]];
firstImage.frame = CGRectMake(0, 0, 100, 100);
UIImageView *secondImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage"]];
secondImage.frame = CGRectMake(100, 0, 100, 100);
[self.view addSubview:firstImage];
[self.view addSubview:secondImage];
}
您可以通过UIImageViews
在视图中添加两个来做到这一点。
如果您已经在显示一个UIImageView
,那么只需添加另一个并将第二个图像放入该图像中。
使用以下代码合并两个图像
#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