2010-05-17 58 views
0

我正在工作的iPhone應用程序,我正在給用戶的iPhone下載圖像的選項。以下是我下載圖像的代碼。如何從iPhone應用程序中刪除圖像

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlAddress]]]; 
NSString *pngFilePath = [[[NSString stringWithFormat:@"%@/",docDir] stringByAppendingString:[NSString stringWithFormat:@"%@",couponID]] stringByAppendingString:@".png"]; 
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; 
[data1 writeToFile:pngFilePath atomically:YES]; 

我上面的代碼工作正常,但現在我想給刪除從上面的代碼下載的圖像的選項。可以有人請指教我如何從iPhone刪除圖像文件。

回答

2

您可以使用removeItemAtPath方法http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/removeItemAtPath:error:。

NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager removeItemAtPath:pngFilePath error:NULL]; 

返回值

是如果刪除操作是成功 。如果操作不 成功,但代表返回從 fileManager:shouldProceedAfterError:removingItemAtPath: 消息 YES,removeItemAtPath:error:也 回報YES。否則,此方法 返回NO

+0

謝謝texmex,我已經完成了使用你的幫助 – pankaj 2010-05-17 10:05:24

1
NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager removeItemAtPath:pngFilePath error:NULL]; 
+0

感謝kenny它幫了我 – pankaj 2010-05-17 10:04:36