gpt4 book ai didi

objective-c - 在 iOS 中保存图像

转载 作者:行者123 更新时间:2023-11-29 11:14:31 38 4
gpt4 key购买 nike

我有一个像照片库一样运行的应用程序。我正在为用户实现删除照片的功能,方法是在每个 UIImageView 上放置一个不可见的按钮,并在他们点击按钮时调用 removeObject。此代码运行良好,但它依赖于标签。我需要在界面生成器中标记每个 UIImageView/UIButton 才能使其工作。因此,我现在尝试以我的标签仍然有效的方式保存图像,这不包括使用 NSData。

所以我完全不知道现在该做什么。我对编程非常非常陌生,我什至对我能做到这一点感到震惊。非常感谢任何有关编辑我的代码的内容或方式以完成这项工作的帮助或建议,谢谢!

这是我的整个文件,仅供引用:

- (IBAction)grabImage {
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

else {
[self presentModalViewController:imgPicker animated:YES];
}
[self.imgPicker resignFirstResponder];
}
// Sets the image in the UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;

[self.array addObject:imageView.image];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;

}

if (imageView2.image == nil) {
imageView2.image = img;
NSLog(@"The image is a %@", imageView);
[self.array addObject:imageView2.image];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}

if (imageView3.image == nil) {
imageView3.image = img;

[self.array addObject:imageView3.image];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}

if (imageView4.image == nil) {
imageView4.image = img;

[self.array addObject:imageView4.image];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView5.image == nil) {
imageView5.image = img;

[self.array addObject:imageView5.image];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}

- (void)applicationDidEnterBackground:(UIApplication*)application {
NSLog(@"Image on didenterbackground: %@", imageView);

[self.array addObject:imageView.image];
[self.array addObject:imageView2.image];
[self.array addObject:imageView3.image];
[self.array addObject:imageView4.image];
[self.array addObject:imageView5.image];

[self.user setObject:self.array forKey:@"images"];
[user synchronize];

}

- (void)viewDidLoad
{
self.user = [NSUserDefaults standardUserDefaults];
NSLog(@"It is %@", self.user);
self.array = [[self.user objectForKey:@"images"]mutableCopy];
imageView.image = [[self.array objectAtIndex:0] copy];
imageView2.image = [[self.array objectAtIndex:1] copy];
imageView3.image = [[self.array objectAtIndex:2] copy];
imageView4.image = [[self.array objectAtIndex:3] copy];
imageView5.image = [[self.array objectAtIndex:4] copy];



UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];

[super viewDidLoad];

}


// This is when the user taps on the image to delete it.
- (IBAction)deleteButtonPressed:(id)sender {
NSLog(@"Sender is %@", sender);
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
message:@"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[deleteAlertView show];

int imageIndex = ((UIButton *)sender).tag;
deleteAlertView.tag = imageIndex;
}

- (UIImageView *)viewForTag:(NSInteger)tag {
UIImageView *found = nil;
for (UIImageView *view in self.array) {
if (tag == view.tag) {
found = view;
break;
}
}
return found;
}

- (void)alertView: (UIAlertView *) alertView
clickedButtonAtIndex: (NSInteger) buttonIndex
{


if (buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
NSLog(@"The tag is %i", alertView.tag);

UIImageView *view = [self viewForTag:alertView.tag];
if (view) {
[self.array removeObject:view];
}

NSLog(@"After deleting item, array count = %d", [array count]);
NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}

[self.user setObject:self.array forKey:@"images"];
}

@end

最佳答案

您似乎正在使用该标签,以便您可以识别正在选择哪个 ImageView 。每张图片上方都有一个“隐形”按钮?是对的吗?我假设这是为了让您可以处理点击,选择通过按钮显示的图像?

有很多方法可以做到这一点,但一个简单的解决方案是只识别水龙头,然后“找到”水龙头下方的 ImageView 。从 Storyboard中将 UITapGestureRecognizer 拖放到 Controller 上。按住 Ctrl 键将它拖到 Controller 的代码中,它会创建一个操作方法。像这样填写它......

  - (IBAction)tapGesture:(UITapGestureRecognizer*)gesture
{
CGPoint tapLocation = [gesture locationInView: self.galleryView];
for (UIImageView *imageView in self.galleryView.subviews) {
if (CGRectContainsPoint(imageView.frame, tapLocation)) {
// This is the imageView that was tapped on!
// Do whatever you want with it now that you found it.
}
}
}

关于objective-c - 在 iOS 中保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050645/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com