How i can save images from this scrollview in photo library when user longpress on imageview.
Getting yellow warning for statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
How can i resolve this warning to save image on photo library
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; 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.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
gestureRecognizer.minimumPressDuration = 2.0;
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
[actionSheet release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
break;
default:
break;
}
}
Getting warning in yellow on this statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
that incompatible pointer types passing UIScrollView to parameter of type UIImage
Thanks
see
this : saving image
and
this :download and saving image
but this one have great rating prev. answer
Related
I m trying to do is to save the current image from the scrollview to photo album. Tried so many different ways and it always saves last image from the scrollview to photo album. Not getting what i m missing in the code.
- (void)viewDidLoad
{
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {
CGFloat xOrigin = i * 320;
pictures = [[NSArray alloc] initWithObjects:#"image0.png", #"image1.png", #"image2.png", #"image3.png", #"image4.png", #"image5.png", #"image6.png", #"image7.png", #"image8.png", #"image9.png", #"image10.png", #"image11.png", #"image12.png", #"image13.png", #"image14.png", #"image15.png", #"image16.png", #"image17.png", #"image18.png", #"image19.png", #"image20.png", #"image21.png", #"image22.png", #"image23.png", #"image24.png", #"image25.png", #"image26.png", #"image27.png", #"image28.png", #"image29.png", #"image30.png", #"image31.png", #"image32.png", #"image33.png", #"image34.png", #"image35.png", #"image36.png", #"image37.png", #"image38.png", #"image39.png", #"image40.png", #"image41.png", #"image42.png", #"image43.png", #"image44.png", #"image45.png", #"image46.png", #"image47.png", #"image48.png", #"image49.png", #"image50.png", #"image51.png", #"image52.png", #"image53.png", #"image54.png", #"image55.png", #"image56.png", #"image57.png", #"image58.png", #"image59.png", #"image60.png", nil];
_image = [UIImage imageNamed:[pictures objectAtIndex:i]];
_imageView = [[[UIImageView alloc] initWithImage:_image]autorelease];
_imageView.tag = i+1;
_imageView.image = _image;
_imageView.frame = CGRectMake(xOrigin, 0, 320, 480);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:_imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(320 * 61 , 480);
[self.view addSubview:imageScrollView];
}
- (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 performSelector:#selector(LongPress:) withObject:nil];
break;
default:
break;
}}
- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
NSInteger currentIndex = roundf(_imageScrollView.contentOffset.x / 320) + 1;
//UIImageView *currentImage = [_imageScrollView viewWithTag:currentIndex];
UIImage* currentImage = [(UIImageView*)[_imageScrollView viewWithTag:currentIndex] image];
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
Edit:
I tried this howcome this is not working either
- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if([pictures objectAtIndex:0]) {
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
} else if([pictures objectAtIndex:1]) {
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
Appreciate help.
Thanks
You have 2-3 options to do so. WIll tell you the option worked perfectly for me.
Just subclass the UIScrollView class, localize your UIImageView object (means declare it in for loop itself.), assign the tag to each imageview. make userInteraction enabled to UIImageView otherwise it won't detect touches, then write touchesBegan method in UIScrollView subclass & in current class. Then write the code whatever you want in handling GestureRecognizer. And then call the touchesBegan of current class from touchesBegan method of UIScrollView.
Try yourself, learn yourself.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Tracing the exact location of the longpressgesture with CGPoint
It always gives NSLog for image pressed 0. I'm don't understand what it is exactly what I'm doing wrong. It never gives me the number of the image I'm pressing. Even when I try to save the image I tap on it always saves the very last image.
- (void)viewDidLoad
{
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
_image = [UIImage imageNamed:imageName];
_imageView = [[[UIImageView alloc] initWithImage:_image]autorelease];
_imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:_imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(_imageView.frame.size.width * 61 , _imageView.frame.size.height);
[self.view addSubview:imageScrollView];
}
- (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 performSelector:#selector(LongPress:) withObject:nil];
break;
default:
break;
}}
- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
UIView *view = gestureRecognizer.view;
int index = view.tag;
//UIImageWriteToSavedPhotosAlbum(index, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
NSLog(#"image pressed %i", index);
}
Because you never set the tag for the view so it defaults to 0. And seriously, how many times are you going to ask the same question? You've been given quite a lot of good feedback, use it.
Tracing the exact location of the longpressgesture with CGPoint
contentoffset in scrollview
Work out what image the scroll view is currently on
detect image object user is viewing in scrollviewdidscroll
Currently it is saving all images but i want to save only the one which is long pressed not all images in one shot only the one user selects
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
//[myButton addGestureRecognizer:tap];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
_imageView.tag = 128;
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
//imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
//[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (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 {
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
}
How to code for that
Thanks for help
In the handleLongPress: function, use this:
UIGestureRecognizer *gestureRecognizer;
CGPoint gesturePoint = [gestureRecognizer locationInView:self.view];
Then check all of your buttons using a for loop like you have there. Use this to check if it is the pressed image:
if(CGRectContainsPoint(myButton.frame,gesturePoint)){
//save the image stored in myButton
//break will exit the for loop early, so if the view comes early in the list,
//it doesn't have to check the rest.
break;
}
I m trying to save image from scrollview to photo album when user long press on image, action sheet shows up with save photo button and when hitting save button it is not saving image to photo album
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
//[myButton addGestureRecognizer:tap];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
_imageView.tag = 128;
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
//imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
//[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
[_imageScrollView viewWithTag:128];
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);//When i add breakpoint at this statement in result it shows storage zero but alert view says success photo got saved in photo album
break;
default:
break;
}
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL)
{
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
} else{
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Please help.
Thanks
It may be an issue with privacy settings - people have been having this problem with my app and no error message is reported. In theory attempting to save a photo should automatically ask the user if the app should be allowed to access their photos however in some cases the user will need to go to Settings > Privacy > Photos and ensure your app is allowed.
How can i set the contentoffset for image to track down which image user is on and selected to save it to phot album.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
_image = [UIImage imageNamed:imageName];
_imageView = [[UIImageView alloc] initWithImage:_image];
_imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:_imageView];
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
Edit:
imageScrollView.contentOffset = CGPointMake(CGFloat x, CGFloat y);
in the contentoffset not sure what to put so that image can be tracked the user is on and selected to save to photo album
- (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{
CGPoint location = [gesture locationInView:_imageView];
if (CGRectContainsPoint(_imageView.bounds, location)){
UIImageWriteToSavedPhotosAlbum(_image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}}}
Any ideas will be appreciated.
You can use this code (that comes from one of the apple sample projects for scrollview) to determine what the currently visible 'page' is in the scrollview.
// Calculate which page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
Once you know this index number you can use it to determine what image to save