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 は 2 つの異なる変数です。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 に答える