因此,我正在編寫的應用程序允許用戶從其相機膠捲上傳照片,並將其顯示在UIImageView中。我有一個「保存」按鈕,可以在按下時保存圖像。我也有一個「編輯」按鈕,點擊時,它允許用戶點擊照片,它將被刪除。這是我遇到的問題,圖像從視圖中刪除,但似乎沒有從數組中刪除。當我關閉並重新啓動應用程序時,照片再次出現。我對Objective-C非常陌生,我很驚訝我甚至做到了這一點,所以我在解決如何從數組中刪除它的問題。另外,作爲一個附註,如果我從相機膠捲中抓取3張圖像,刪除中間的一張,然後按下保存按鈕,關閉並重新啓動應用程序後,它只會顯示應該顯示的2張照片。關於重新啓動後圖像再次出現的問題僅在先按下保存,然後嘗試刪除它們時纔會出現。從NSMutableArray中刪除圖像
這就是我要如何刪除它們。我在每個UIImageView上都有一個不可見的按鈕,當按下Edit按鈕時,它將取消隱藏它們。我已經標記這兩個按鈕和UIImageViews的,並且當按下一個按鈕,這個火災:
- (IBAction)deleteButtonPressed:(id)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]) {
UIImageView *view = [self viewForTag:alertView.tag];
if (view) {
[self.array removeObject:view];
}
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}
[self.user setObject:self.array forKey:@"images"];
}
而且僅作參考,這是我如何保存圖像:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
[self.array addObject:imageView];
[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];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
...
...
}
-(IBAction)saveButtonPressed:(id)sender {
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];
for (UIImageView *imageViewToSave in self.array) {
NSInteger tag = imageViewToSave.tag;
UIImage *image = imageViewToSave.image;
NSString *imageName = [NSString stringWithFormat:@"Image%i.png",tag];
NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:NO];
}
}
這是我如何加載它們:
-(void)viewDidLoad {
self.array = [NSMutableArray array];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSArray *docFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:docsDir error:NULL];
for (NSString *fileName in docFiles) {
if ([fileName hasSuffix:@".png"]) {
NSString *fullPath = [docsDir stringByAppendingPathComponent:fileName];
UIImage *loadedImage = [UIImage imageWithContentsOfFile:fullPath];
if (!imageView.image) {
imageView.image = loadedImage;
}
else if (!imageView2.image) {
imageView2.image = loadedImage;
}
else if (!imageView3.image) {
imageView3.image = loadedImage;
}
}
}
}
更新:這裏是我當前的代碼:
- (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]) {
UIImageView *view = [self viewForTag:alertView.tag];
if (view) {
[self.array removeObject:view];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%i.png",view.tag]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:NULL];
}
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}
[self.user setObject:self.array forKey:@"images"];
}
感謝您的回答。所以,我應該添加[fileManager removeItemAtPath:查看錯誤:NULL];在[self.array removeObject:view];?下對於新手問題抱歉,對此仍然很陌生。 – John 2012-04-16 18:02:48
我編輯了答案來顯示一個例子。 – sooper 2012-04-16 18:27:42
非常感謝。對於'stringByAppendingPathComponent',我應該簡單地使用'view'嗎?我使用'view'來確定哪個UIImageView被刪除的標籤。 – John 2012-04-17 01:27:06