2

我想UIImageView在一个UIView.

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

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

我想为UIImageView用户选择的每个图像写一个并让他们留在视图上。

如何添加多个UIImageViewsUIView从图像中挑选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];
4

3 回答 3

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,但这又取决于您。我可能倾向于使用CoreDataorSQLite或 aplist来存储这组图像(或者,更准确地说,将文件名存储在那里,并将图像保存在Documents文件夹中)。但是使用NSUserDefaults要求我们遍历假设的键名,这是脆弱的(例如,如果您有 3 张图像然后去掉第二张图像怎么办……您是否要重命名所有键以避免键名中的间隙)。但是,如果您使用 a plistor 使用CoreDataor SQLite,您可以遍历图像集合,而不必担心缺少键名。

无论如何,希望这能让您了解如何更动态地检索图像。

于 2012-12-24T06:14:40.463 回答
1

这是答案;如何向一个 UIView 添加多个 UIImageView;从图像列表中。

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

创建视图控制器.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;

创建视图控制器.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 中引用,用于可拖动图像。

项目UIImageView.h

#import <UIKit/UIKit.h>

@interface ItemUIImageView : UIImageView {
    CGPoint startLocation;

}

@end

项目UIImageView.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

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

项目.h

#import <Foundation/Foundation.h>

@interface Item : NSObject

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

@end

项目.m

#import "Item.h"

@implementation Item

@synthesize filename, category;

@end

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

于 2012-12-27T06:29:18.080 回答
0

看看这个示例代码

来自developer.apple.com

这会对你有很大帮助。

于 2012-12-24T06:13:13.097 回答