将照片保存到相册

时间:2012-10-14 16:23:25

标签: iphone uiscrollview uiimageview ios6

如果我在我的应用中显示多个这样的图像

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

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

imageView和_imageView是两个不同的变种。你正在设置imageView的图像,但是当你想要获取它时,你会发送消息_imageView,它的图像属性你从未设置过。

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

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