2

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];
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参照を保存しています。あなたがそれをするかどうかはあなた次第です。NSMutableArrayimageViews

そうは言っても、私が完全に理解していないコードの部分があります。たとえば、すべてUIImageViewが同じフレームを持っています...お互いに隠される画像ビューを作成する理由はわかりませんが、それは君による。

また、画像の配列をに格納する傾向はありませんがNSUserDefaults、これもあなた次第です。私はおそらく、CoreDataまたはSQLiteまたはを使用plistしてこの画像の配列を保存する傾向があります(または、より正確には、ファイル名をそこに保存し、画像をDocumentsフォルダーに保持します)。ただし、使用NSUserDefaultsするには、壊れやすい架空のキー名を繰り返す必要があります(たとえば、3つの画像があり、2番目の画像を削除した場合、キー名のギャップを避けるためにすべてのキーの名前を変更しますか? )。ただし、plistまたはを使用するCoreData場合SQLiteは、キー名の欠落を心配することなく、画像のコレクションを反復処理できます。

とにかく、うまくいけば、これは画像をより動的に取得する方法についてのアイデアをあなたに与えるでしょう。

于 2012-12-24T06:14:40.463 に答える
1

これが答えです。UIViewに複数のUIImageViewを追加する方法。画像のリストから。

以下のコードは現在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];
}

これは、ドラッグ可能な画像用にCreateViewController.hおよび.mで参照されるサブクラスUIImageViewファイルです。

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

ロブが助けた後、私が自分の質問に答えるかどうかはわかりません。スタックで完全な例を見つけられなかったので、ここに私のものがあります。

于 2012-12-27T06:29:18.080 に答える
0

このサンプルコードをご覧ください。

developer.apple.comから

それはあなたに大いに役立ちます。

于 2012-12-24T06:13:13.097 に答える