开发者

stop Saving Image to photo library

开发者 https://www.devze.com 2023-03-20 07:43 出处:网络
How to stop saving image.which is already existin iphone photo library. This code is for savingImage.....

How to stop saving image. which is already exist in iphone photo library.

This code is for savingImage.....

-(IBAction)saveImage:(id)sender 
{

    NSLog(@"calling save");

    if (coverPage !=nil) 
    {
        NSData * imageData = UIImagePNGRepresentation(coverPage);
        UIImage *theImage = [UIImage imageWithData:imageData];
        UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
    }

    else {
        UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Image is nil" delegat开发者_如何转开发e:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"OK",nil];
        [alertView show];
        [alertView release];
    }
}

if image is saved and clicking again and again the photo is saving..... How to stop saving image once it saved.


Without having more context it is difficult to say, but broadly speaking you can use a variable to track whether or not you have previously saved the current coverPage and simply avoid re-saving it if you have. For example:

-(IBAction)saveImage:(id)sender{
    NSLog(@"calling save");

    if (coverPageAlreadySaved) {
        UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Silly user, you already saved this image." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
        [alertView show];
        [alertView release];
    }
    else if (coverPage !=nil) {
        NSData * imageData = UIImagePNGRepresentation(coverPage);
        UIImage *theImage = [UIImage imageWithData:imageData];
        UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
        coverPageAlreadySaved = YES;
    }
    else {
        UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Image is nil" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
        [alertView show];
        [alertView release];
    }
}

...just be sure to set coverPageAlreadySaved = NO whenever the coverPage is changed (wherever you happen to be doing that in your code).

Alternately, since you already have the check against nil, you could just do:

-(IBAction)saveImage:(id)sender{
    NSLog(@"calling save");

    if (coverPage !=nil) {
        NSData * imageData = UIImagePNGRepresentation(coverPage);
        UIImage *theImage = [UIImage imageWithData:imageData];
        UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
        coverPage = nil;
    }
    else {
        UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Image is nil" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
        [alertView show];
        [alertView release];
    }
}

...which will prevent the image from being re-saved until it gets updated somewhere else in your code (at which point I assume it counts as a new coverPage).


If it's just one image you're saving then you can perform this just with a BOOL ivar for flag, like:

BOOL imageSaved;

And whenever you tap the button it'll just check if !imageSaved then perform the saving. But if it's more then one image then you can do this using NSMutableArray.Like:

NSMutableArray *savedImages;//it's an ivar

Then before performing the saving operation:

NSString *imageName=......//Here I asssume you can get the image's name
if(!imageSaved)
 NSMutableArray *savedImages=[[NSMutableArray alloc]init];
if (![savedImages containsObject:imageName]){

.........here comes the saving operation and at the end you add the imageName to the array

 [savedimages addObject:imageName];
 }

I've given you just an example. You might not know the names of the images you save. If that's the case you can mark the image (which can be performed in several ways) and add the image itself.


Set the UIButton as an IBOutlet as well. Then set the enabled property to NO in the IBAction. Set the UIButton IBOutlet back to YES somewhere else in your program to 'reset' the button. It is similar to using a UIStepper in that you have to declare it twice...once as an IBAction and once as an IBOutlet.

This will allow you to use the button once, then it is 'greyed out' or 'muted' to prevent further activation. Basically you are disabling the UIGestureRecognizer from the UIButton.

Set the IBAction and IBOutlet.

- (IBAction)saveImageButton:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *saveImageButton;

Set the enabled to NO in the IBAction.

- (IBAction)saveImageButton:(id)sender {
UIImageWriteToSavedPhotosAlbum(_processedImageView.image, nil, nil, nil);
_saveImageButton.enabled = NO;

}

Then change it to YES somewhere else in your program to 'reset' the button so you can use it again.

_saveImageButton.enabled = YES;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号