2012-10-14 179 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

相關問題