2011-12-12 62 views
1

我最近檢查項目「演示照片局」發現here保存多個自定義的UIImageViews

這是將UIImageView添加到添加了UIGestureRecognizers的屏幕的簡單演示...允許用戶操作各種UIImageView。

我加的觀點,像這樣:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 


imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; 
imageview.userInteractionEnabled = YES; 
[imageview setImage:image]; 

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)]; 
[pinchRecognizer setDelegate:self]; 
[imageview addGestureRecognizer:pinchRecognizer]; 

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)]; 
[rotationRecognizer setDelegate:self]; 
[imageview addGestureRecognizer:rotationRecognizer]; 

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
[panRecognizer setMinimumNumberOfTouches:1]; 
[panRecognizer setMaximumNumberOfTouches:1]; 
[panRecognizer setDelegate:self]; 
[imageview addGestureRecognizer:panRecognizer]; 


[self.view addSubview:imageview]; } 

我甚至可以保存像這樣的觀點:

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:imageview] forKey:@"imageViewSaved"]; 

現在我的問題:下面的方法僅保存最後的ImageView添加到屏幕。任何人都知道如何保存屏幕上的所有圖像...如果用戶添加多個圖像?

+0

你不能只是做一樣的,你已經與第一ImageView的呢?我不太確定,如果我完全理解您的問題,您是否問如何將多個imageView存檔添加到您的視圖? –

+0

我提供的例子允許用戶每次從他們的照片庫中選擇照片時在屏幕上添加一個uiimageview。所有這些圖像瀏覽本質上都是「imageview」(見上面的代碼)。我很難弄清楚如何區分這些不同的圖像視圖。目前它只保存添加的最新視圖...即使其他uiimageviews也在屏幕上。我想我每次選擇一張附加照片時都可以在新的圖像視圖中編碼,但是我必須限制允許添加多少個uiimageviews。 – jccotant

回答

0

我真的不認爲你想成爲拯救整個UIImageView對象指向用戶的默認值。相反,請嘗試保存一個UIImage對象的列表,這些對象將更加輕量級。 (而這使得更多的意義連載。)

你可以做到這一點,像這樣:

NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; 
NSArray *savedArray = [defs objectForKey:@"SAVED_IMAGES"]; 
// Create a mutable version in case the serialized copy is an NSArray 
NSMutableArray *mutableImages = savedArray ? [NSMutableArray arrayWithArray:savedArray] : [NSMutableArray array]; 
[mutableImages addObject:image]; 
[defs setObject:mutableImages forKey:@"SAVED_IMAGES"]; 
[defs save]; 
+0

感謝您的回覆。您將整個UIImageView設置爲用戶默認值是正確的。我也很感激你的更好的方式來保存各種圖像的例子。我想澄清的是我如何區分屏幕上的各種UIImageviews。我上面介紹的代碼允許用戶添加多個UIImageviews,名稱均爲「imageview」。 – jccotant

0

爲了區別,你可以做以下的屏幕上的UIImageViews。

定義開始ImageView的標籤號碼

#define kImageViewTag 3000 

定義添加到屏幕圖像的數量實例變種。

NSUInteger numberOfImage = 0 

當你創建一個新的UIImageView,增加了計數,並與kImageViewTag添加它,並把它分配給標記屬性。

imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; 
imageview.userInteractionEnabled = YES; 
[imageview setImage:image]; 
numberOfImage += 1; 
imageView.tag = kImageViewTag + numberOfImage; 

每當你列舉屏幕子視圖,如果類的UIImageView與標籤> = kImageViewTag,你知道這些都是圖像添加到從的UIImagePickerController屏幕。