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);
  }

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

感谢帮助。

4

1 回答 1

1

imageView 和 _imageView 是两个不同的变量。您正在设置 imageView 的图像,但是当您尝试获取它时,您正在向 _imageView 发送消息,您从未设置过它的图像属性。

编辑:这更简单。只需将手势识别器直接分配给图像视图,然后在 handleLongPress: 中获取您的图像视图。在 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);
  }
于 2012-10-14T18:45:40.803 回答