如果我在我的應用程序中顯示多個相似圖片保存照片到相冊
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);
}
但是當執行應用程序時,它不會將任何照片保存到相冊中。我是否錯過了一些重要的信息來放置代碼使其工作。
感謝您的幫助。
您設置圖像的圖像視圖是您每次運行for循環時分配的新實例,但您試圖保存的圖像屬性是ivar上屬性imageView的圖像屬性。我認爲你永遠不會這樣做。 – geraldWilliam
你說得很對。我沒有注意到我的代碼中的小指針。我修正它像這樣imageView.image = [UIImage imageNamed:imageName]; 但仍然無法工作。我對我的帖子進行了更改,請檢查它。感謝幫助。 – user1452248