我有UIScrollView有子視圖(圖片)添加做到這一點。每次用戶觸摸滾動視圖中的圖片時,都會觸發其上的複選標記。捕獲哪個子視圖在UiScrollView中被觸摸但無法在另一個函數中訪問它。
NSMutableIndexSet * picturesArray; < - 宣佈的.h
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
NSLog(@"Touch down");
for (UITouch *touch in touches) {
for (int i = 1; i <= [self subviews].count; i++)
{
if(CGRectContainsPoint([[self viewWithTag:i]frame], [touch locationInView:self])){
NSLog(@"touched %d th view",i);
NSArray *subviews = [[self viewWithTag:i] subviews];
UIImageView *view = nil;
view = [subviews objectAtIndex:0];
if(view.hidden){
// add the index
[picturesArray addIndex:i];
view.hidden = NO; //check mark is shown
}else{
[picturesArray removeIndex:i];
view.hidden = YES; //check mark is not shown
}
// UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- WORKS IF CALLED
}
}
}
}
問題1:這是這樣做的最佳方式?這似乎是使用for(int i = 1;我< = [self subviews] .count; i ++)很慢。我基本上需要捕捉哪個子視圖被觸摸。我沒有想通過除了每個子視圖
保存照片被調用,基本上搜索通過哪些圖片被觸摸並將它們保存到相冊。但是對UIImageWriteToSavedPhotosAlbum的調用失敗。這與TouchesEnded在同一個文件中。但是當在TouchesEnded中調用時,它會起作用。
(IBAction爲)savePhotos:(ID)發送方{
的NSLog(@ 「索引集%@」,picturesArray);
const NSUInteger arrayCount = picturesArray.count;
NSUInteger * theIndexBuffer =(NSUInteger *)calloc(picturesArray.count,sizeof(NSUInteger)); UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:0] image],nil,nil,nil);
[picturesArray getIndexes:theIndexBuffer maxCount:arrayCount inIndexRange:nil];
對(INT I = 0;我< arrayCount;我++){
NSLog(@"Element is %d",theIndexBuffer[i]); UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- THIS CRASHES
}
}
問題2:爲什麼說UIImageWriteToSavedPhotosAlbum失敗?
感謝您的回覆SLF! 你能通過實現UiImageView的孩子來闡述你的意思嗎?對於自動發佈問題 [(UIImageView *)[self viewWithTag:i] image]基本上是NULL。如此自我viewwithTag:我正在自動發佈? – user82383 2009-04-30 20:27:23