If i am displaying multiple images like this in my app
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);
EDIT:
imageView.image = [UIImage imageNamed:imageName];
After adding the above statement to the code still it is not working.
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);
Now after displaying images in scrollview if on any of the image within scrollview is long pressed will show action sheet with save photo button
- (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;
}}
When save photo button is pressed
-(void)savePhoto{
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
}
but when executing the app it is not saving any photo to photo album. Am i missing some important piece of information to put in code to make it work.
Thanks for help.
imageView and _imageView are two different vars. you're setting the image of imageView but when you're trying to get it, you're messaging _imageView, whose image property you never set.
EDIT:This is simpler. Just assign the gesture recognizer directly to the image view and then get your image view in handleLongPress:. Store a ref to that view in a selectedImageView property and then get the image property on that image view when you save. See my edits to your code.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.