Objective c 停止将图像保存到照片库
如何停止保存图像。它已经存在于iphone照片库中 此代码用于保存图像Objective c 停止将图像保存到照片库,objective-c,ipad,Objective C,Ipad,如何停止保存图像。它已经存在于iphone照片库中 此代码用于保存图像 -(IBAction)saveImage:(id)sender { NSLog(@"calling save"); if (coverPage !=nil) { NSData * imageData = UIImagePNGRepresentation(coverPage); UIImage *theImage = [UIImage imageWithData:
-(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" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"OK",nil];
[alertView show];
[alertView release];
}
}
如果保存图像并反复单击,则照片正在保存。。。。。如何在保存图像后停止保存。如果没有更多的上下文,很难说,但广义上讲,您可以使用变量跟踪您以前是否保存过当前的
封面,如果保存过,只需避免重新保存即可。例如:
-(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];
}
}
…只要确保在更改封面
时设置coverPage readysaved=NO
(无论您在代码中何时这样做)
或者,由于您已经对nil
进行了检查,因此您可以执行以下操作:
-(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];
}
}
…这将防止图像被重新保存,直到它在代码中的其他地方被更新为止(此时我假定它将被视为新的封面)。如果它只是一个图像,则可以使用BOOL ivar for标志执行此操作,如:
BOOL imageSaved;
无论何时你点击按钮,它都会检查是否!imageSaved然后执行保存。
但如果是多个图像,则可以使用NSMutableArray执行此操作。例如:
NSMutableArray *savedImages;//it's an ivar
然后,在执行保存操作之前:
NSString *imageName=......//Here I asssume you can get the image's name
if(!imageSaved)
NSMutableArray *savedImages=[[NSMutableArray alloc]init];
if (![savedImages containsObject:imageName]){
……下面是保存操作
最后,将imageName添加到数组中
[savedimages addObject:imageName];
}
我给你举了个例子。您可能不知道保存的图像的名称。如果是这种情况,您可以标记图像(可以通过多种方式执行)并添加图像本身。将ui按钮设置为IBOutlet
。然后在iAction中将enabled属性设置为NO。在程序中的其他地方将UIButton IBOutlet设置回“是”,以“重置”按钮。它类似于使用UIStepper
,因为您必须声明它两次…一次作为iAction
,一次作为iOutlet
- (IBAction)saveImageButton:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *saveImageButton;
这将允许您使用该按钮一次,然后将其“灰显”或“静音”以防止进一步激活。基本上,您是从ui按钮
禁用uigestureRecognitor
设置IBAction
和IBOutlet
- (IBAction)saveImageButton:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *saveImageButton;
在iAction中将enabled设置为NO
- (IBAction)saveImageButton:(id)sender {
UIImageWriteToSavedPhotosAlbum(_processedImageView.image, nil, nil, nil);
_saveImageButton.enabled = NO;
}
然后在程序的其他地方将其更改为“是”,以“重置”按钮,以便再次使用
_saveImageButton.enabled = YES;
“黑暗”的一面是,如果用户直接从照片库中删除图像,应用程序将不会收到任何更新存储值的通知。