我有添加了子视图(图片)的 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; i <= [self subviews].count; i++) 很慢。我基本上需要捕捉被触摸的子视图。除了通过每个子视图之外,我还没有弄清楚这一点
调用 savePhotos 并基本上搜索触摸了哪些图片并将它们保存到相册。但是,对 UIImageWriteToSavedPhotosAlbum 的调用失败。这与 TouchesEnded 在同一个文件中。但是当在 TouchesEnded 中调用时,它可以工作。
(IBAction) savePhotos: (id) sender{
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];
for(int i = 0; i < arrayCount; i ++){
NSLog(@"Element is %d",theIndexBuffer[i]); UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- THIS CRASHES
}
}
问题 2:为什么 UIImageWriteToSavedPhotosAlbum 失败了?