2012-10-14 133 views
0

如果我在我的应用程序中显示多个相似图片保存照片到相册

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
imageScrollView.pagingEnabled = YES; 
NSInteger numberOfPhotos = 61; 

for (int i = 0; i < numberOfPhotos; i++) { 
    CGFloat xOrigin = i * self.view.frame.size.width; 

NSString *imageName = [NSString stringWithFormat:@"image%d.png", i]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    _imageView.tag = 122; 
    imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height); 

编辑:

 imageView.image = [UIImage imageNamed:imageName]; 

添加上述语句中的代码后它仍然是行不通的。

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] 
                 initWithTarget:self 
                 action:@selector(handleLongPress:)]; 

    imageScrollView.userInteractionEnabled = YES; 
    [imageScrollView addGestureRecognizer:gestureRecognizer]; 
    gestureRecognizer.delegate = self; 
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfPhotos, self.view.frame.size.height); 

现在,在滚动视图显示图像之后,如果在任何滚动视图内的图像长按将显示动作片与保存照片按钮

- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{ 
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil]; 
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 

}} 


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
switch (buttonIndex) { 
    case 0: 

     [self savePhoto]; 

     break; 

    default: 
     break; 

}} 

节省当照片按钮被按下

-(void)savePhoto{ 


    UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil); 
    } 

但是当执行应用程序时,它不会将任何照片保存到相册中。我是否错过了一些重要的信息来放置代码使其工作。

感谢您的帮助。

+0

您设置图像的图像视图是您每次运行for循环时分配的新实例,但您试图保存的图像属性是ivar上属性imageView的图像属性。我认为你永远不会这样做。 – geraldWilliam

+0

你说得很对。我没有注意到我的代码中的小指针。我修正它像这样imageView.image = [UIImage imageNamed:imageName]; 但仍然无法工作。我对我的帖子进行了更改,请检查它。感谢帮助。 – user1452248

回答

1

imageView和_imageView是两个不同的变量。您正在设置imageView的图像,但是当您尝试获取图像时,您正在传递_imageView,其图像属性您从未设置过。

编辑:这更简单。只需将手势识别器直接分配给图像视图,然后在handleLongPress:中获取图像视图。将ref保存到selectedImageView属性中,然后在保存时在该图像视图中获取图像属性。查看我对编码的编辑。

Declare 

//a property to store a reference to the image view that the user selected 
@property (strong, nonatomic) UIImageView *selectedImageView; 

in viewDidLoad: self.imageViews = [[NSMutableArray alloc] init]; 

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
imageScrollView.pagingEnabled = YES; 
NSInteger numberOfPhotos = 61; 

for (int i = 0; i < numberOfPhotos; i++) { 
    CGFloat xOrigin = i * self.view.frame.size.width; 

NSString *imageName = [NSString stringWithFormat:@"image%d.png", i]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    //make tag an incremented variable to ensure that all of the imageViews have a different tag 
    _imageView.tag = i; 

    imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height); 


    UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] 
                                                       initWithTarget:self 
                                                       action:@selector(handleLongPress:)]; 

    //I would add the gesture recognizer directly to the image view at this point. 
    imageView.userInteractionEnabled = YES; 
    [imageView addGestureRecognizer:gestureRecognizer]; 
    gestureRecognizer.delegate = self; 

imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfPhotos, self.view.frame.size.height); 
} 

- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{ 
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ 
//get the image view that the user selected and save it as your selectedImageView property 
UIImageView *pressedImageView = (UIImageView *)gestureRecognizer.view; 
self.selectedImageView = pressedImageView; 

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil]; 
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 

}} 


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
switch (buttonIndex) { 
    case 0: 

        [self savePhoto]; 

       break; 

    default: 
        break; 

}} 


-(void)savePhoto{ 

//get the image from the imageView that you stored a reference to when the user selected it 
  UIImageWriteToSavedPhotosAlbum(self.selectedImageView.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil); 
  } 
+0

如果您可以请详细说明更多的代码条款,我将不胜感激。 – user1452248

相关问题