7

UIImageViews水平スクロールビューに複数の画像()があるiPadアプリを作成しています。ユーザーがのいずれかをタップしたときに、画像をフォトライブラリに保存できるようにしたいと思いますUIImageView。Safariがこの問題を処理する方法が好きです。ポップアップメニューが表示されるまでタップして押し続け、[画像を保存]をクリックするだけです。「」があることは知っていUIImageWriteToSavedPhotosAlbumます。しかし、私はiOS開発の初心者であり、どこに行くのか、どこに置くのか(つまり、どの画像がタップされたかを検出する方法)がよくわかりません。

私が見つけたものから、私は人々がUIImageの代わりに使用するのを見ましUIImageViewた。ビューをに変換する必要がありUIImageますか?もしそうなら、どのように?ユーザーが画像をタップしたとき、およびタップされた画像を検出するにはどうすればよいUIImageViewですか?あなたが私を正しい方向に向けることができれば、そして多分いくつかの例を私はそれを大いに感謝します。

4

4 に答える 4

32

imageaのプロパティを使用してUIImageView、現在の画像を取得できます。

UIImage* imageToSave = [imageView image]; // alternatively, imageView.image

// Save it to the camera roll / saved photo album
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
于 2010-11-16T19:43:03.737 に答える
4
- (IBAction)TakePicture:(id)sender {


    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;


    // Delegate is self
    imagePicker.delegate = self;


    OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];

   // imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);

    // Insert the overlay:
    imagePicker.cameraOverlayView = overlay;

   // Allow editing of image ?
    imagePicker.allowsImageEditing = YES;
    [imagePicker setCameraDevice:
     UIImagePickerControllerCameraDeviceFront];
    [imagePicker setAllowsEditing:YES];
    imagePicker.showsCameraControls=YES;
    imagePicker.navigationBarHidden=YES;
    imagePicker.toolbarHidden=YES;
    imagePicker.wantsFullScreenLayout=YES;


    self.library = [[ALAssetsLibrary alloc] init];


    // Show image picker
    [self presentModalViewController:imagePicker animated:YES];
}





- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];




    // Save image to album
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);


    // save image to custom album
    [self.library saveImage:image toAlbum:@"custom name" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];

    [picker dismissModalViewControllerAnimated:NO];


}



- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

    // Unable to save the image  
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                           message:@"Unable to save image to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                           message:@"Image saved to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];


    [alert show];
}



- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // After saving iamge, dismiss camera
    [self dismissModalViewControllerAnimated:YES];
}
于 2012-11-06T15:06:55.080 に答える
3

どのUIImageViewがタップされたかを検出する方法を尋ねる質問の部分に関しては、次のようなコードを使用できます。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

 [super touchesEnded:touches withEvent:event];
 UITouch *touch = [touches anyObject];
 CGPoint touchEndpoint = [touch locationInView:self.view]; 
 CGPoint imageEndpoint = [touch locationInView:imageview];
 if(CGRectContainsPoint([imageview frame], touchEndpoint))
 {
 do here any thing after touch the event.

  }
}
于 2011-07-28T19:27:56.977 に答える
1

Swiftの場合

    // Save it to the camera roll / saved photo album
    // UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, nil, nil, nil) or 
    UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, self, "image:didFinishSavingWithError:contextInfo:", nil)

    func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
            if (error != nil) {
                // Something wrong happened.
            } else {
                // Everything is alright.
            }
    }
于 2015-03-18T18:12:09.497 に答える