如何将多个UIImageViews添加到UIView;从图像列表

时间:2012-12-24 05:04:55

标签: iphone objective-c ios uiview uiimageview

我想在UIImageView中在彼此的顶部添加多个UIView

我正在构建视图,然后成功保存到库。我从相机或库中添加了一张用于背景的图片,然后在其上添加了多张图片;来自TableListController。 在viewDidLoad我为每个可用图片写UIImageView;和视图仅出现在用户从TableListController选择的图像上。

这是一个有限的解决方案。当用户选择相同的图像;图像被覆盖。我希望能够在视图上复制图像。

我想为用户选择的每个图片写一个UIImageView并让它们留在视图中。

如何将多个UIImageViews添加到UIView;从TableListController

中挑选的图像

这就是我现在所拥有的。我需要一个更有活力的解决方案。

CGRect myFrame = CGRectMake(0,-20,320,480);
self.createView = [[UIView alloc] initWithFrame:myFrame];
self.createView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.createView];

self.bgImgView = [[UIImageView alloc] initWithImage:image];
self.bgImgView.frame = CGRectMake(0,0,320,480);
[self.createView addSubview:self.bgImgView];


NSData *imageDataz = [[NSUserDefaults standardUserDefaults] objectForKey:@"catzero-item-0"];
UIImage *myPickZero = [UIImage imageWithData:imageDataz];
UIImageView *test0 = [[UIImageView alloc] initWithImage:(UIImage *)myPickZero];
test0.frame = CGRectMake(0,0,320,480);
[self.createView addSubview:test0];

NSData *imageDatao = [[NSUserDefaults standardUserDefaults] objectForKey:@"catzero-item-1"];
UIImage *myPickOne = [UIImage imageWithData:imageDatao];
UIImageView *test1 = [[UIImageView alloc] initWithImage:(UIImage *)myPickOne];
test1.frame = CGRectMake(0,0,320,480);
[self.createView addSubview:test1];

3 个答案:

答案 0 :(得分:3)

如果你想做一些更有活力的事情,你可以这样做:

CGRect frame = self.view.bounds; // probably better to get the parent's bounds, rather than hardcoding frame dimensions

CGRect myFrame = frame;
myFrame.origin.y -= 20; // I'm not sure why you're offsetting this by 20, but you really do, grab the superview's bounds and offset it
self.createView = [[UIView alloc] initWithFrame:myFrame];
self.createView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.createView];

self.bgImgView = [[UIImageView alloc] initWithImage:image];
self.bgImgView.frame = frame;
[self.createView addSubview:self.bgImgView];

self.imageViews = [[NSMutableArray alloc] init];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger i = 0;
NSData *data;

while ((data = [userDefaults objectForKey:[NSString stringWithFormat:@"catzero-item-%d", i++]]) != nil)
{
    UIImage *image = [UIImage imageWithData:data];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = frame;
    [self.createView addSubview:imageView];
    [self.imageViews addObject:imageView];
}

这将读取以@"catzero-item-"为前缀的键的图像,并为它们创建图像视图。我还将单个UIImageView引用保存到我创建的NSMutableArray属性imageViews中。你是否这样做取决于你。

话虽如此,你的代码中有一部分我不太了解:例如,每个UIImageView都有相同的框架...我不知道为什么你要创建图像视图相互遮挡,但这取决于你。

我也不倾向于在NSUserDefaults中存储一系列图像,但这又取决于你。我可能倾向于使用CoreDataSQLiteplist来存储此图像数组(或者,更准确地说,将文件名存储在那里,并将图像保存在{ {1}}文件夹)。但是使用Documents要求我们迭代假设的密钥名称,这是脆弱的(例如,如果你有3个图像,然后摆脱第二个...你要重命名所有密钥以避免关键名称的差距)。但是,如果您使用NSUserDefaults或使用plistCoreData,则可以遍历图像集合,而无需担心缺少关键名称。

无论如何,希望这能为您提供有关如何更动态地检索图像的想法。

答案 1 :(得分:1)

这是答案;如何将多个UIImageViews添加到UIView;从图像列表中。

以下代码目前在iOS6中运行:

CreateViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "Item.h"
#import "PickTableViewController.h"
#import "CreateViewController.h"
@interface CreateViewController : UIViewController {
    CGPoint startLocation;
}


@property(nonatomic, strong)UIView *createView;
@property(nonatomic, strong)UIImageView *bgImgView;

@property(nonatomic, strong) Item *currentItem;
@property(nonatomic, strong)UIImage *myPick;
@property(nonatomic, strong)UIImage *myImage;

@property(nonatomic, strong)NSMutableArray *imageViews;
@property(nonatomic, strong)NSMutableArray *imageViewItems;

CreateViewController.m

// create a view on top of base view controller.
CGRect myFrame = CGRectMake(0,-20,320,480);
self.createView = [[UIView alloc] initWithFrame:myFrame];
[self.view addSubview:self.createView];
// This is an pic chosen by user from camera or library or default image.
self.bgImgView = [[UIImageView alloc] initWithImage:image];
[self.createView addSubview:self.bgImgView];

// currentItem is set in TableViewController and passed via prepareforsegue.
NSString *cat = currentItem.category;
NSString *file = currentItem.filename;

// add images to "imageViewItems" array.
imageViewItems = [[NSMutableArray alloc] init];
Item *img = [[Item alloc] init];
[img setCategory:cat];
[img setFilename:file];
[imageViewItems addObject:img];
img = [[Item alloc] init];
[img setFilename:@"catone-item-0.png"];
[imageViewItems addObject:img];
img = [[Item alloc] init];
[img setCategory:@"catone"];
[img setFilename:@"catone-item-1.png"];
[imageViewItems addObject:img];

// create array of uiimageviews from array of images.
CGRect frame = self.view.bounds;
CGRect _myFrame = frame;
for (int i = 0; i < [imageViewItems count]; i++) {
    // get array item.
    Item *arrayItem = [imageViewItems objectAtIndex:i];
    // get filename from array item.
    NSString *curpick = arrayItem.filename;
    // create UIImage from array item filename.
    image = [UIImage imageNamed:curpick];

    // create UIImageView with UIImage
    ItemUIImageView *imageView = [[ItemUIImageView alloc] initWithImage:image];
    imageView.frame = _myFrame;
    imageView.userInteractionEnabled = YES;
    // add subview to createView.
    [self.createView addSubview:imageView];
    // add UIImageView to array.
    [self.imageViews addObject:imageView];
}

这是子类UIImageView文件,在CreateViewController.h和.m中引用,用于可拖动图像。

ItemUIImageView.h

#import <UIKit/UIKit.h>

@interface ItemUIImageView : UIImageView {
    CGPoint startLocation;

}

@end

ItemUIImageView.m

#import "ItemUIImageView.h"

@implementation ItemUIImageView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code


}
return self;
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

CGPoint pt = [[touches anyObject] locationInView:self];
CGFloat dx = pt.x - startLocation.x;
CGFloat dy = pt.y - startLocation.y;
CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy);
self.center = newCenter;
} 




@end

此文件是用于存储图像对象信息的自定义类。

Item.h

#import <Foundation/Foundation.h>

@interface Item : NSObject

@property (nonatomic, strong) NSString *filename;
@property (nonatomic, strong) NSString *category;

@end

Item.m

#import "Item.h"

@implementation Item

@synthesize filename, category;

@end

我不确定在Rob帮忙之后我是否要回答我自己的问题。我没有在堆栈中找到完整的示例,所以这是我的。

答案 2 :(得分:0)

看看这个sample code

来自developer.apple.com

它会对你有所帮助。