2013-10-28 72 views
0

我有圖像文件存儲在iphone文檔目錄中,當我給該圖像按鈕背景它不顯示。如何從iphone文檔文件夾設置按鈕背景

NSString*imageThumbnailVideo= [NSString stringWithFormat:@"%@thumbVideo.png",imageThumbnail]; 

NSLog(@"imageThumbnailVide is %@",imageThumbnailVideo); 

NSString * filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),imageThumbnailVideo]; 

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 
if (fileExists == 0) 
{ 
    NSLog(@"File is Not exists"); 
} 
else 
{ 
    NSLog(@"File is present"); 
} 

[thumbnailImageView setBackgroundImage:[UIImage imageNamed:imageThumbnailVideo] forState:UIControlStateNormal]; 

它還顯示文件存在但不顯示圖像。

這裏是我如何保存文件

testT=[NSString stringWithFormat:@"%@thumbVideo",titleTextField.text]; 

    NSString *newStringVT = [testT stringByReplacingOccurrencesOfString:@" " withString:@""]; 


    NSString*imageTitleVT=[NSString stringWithFormat:@"%@.png",newStringVT]; 


    NSString *pathVT = [documentsDirectory stringByAppendingPathComponent:imageTitleVT]; 


    NSData*thumbVideoData=UIImagePNGRepresentation(thumbnail); 

    NSError * error = nil; 
    [thumbVideoData writeToFile:pathVT options:NSDataWritingAtomic error:&error]; 

回答

0
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [path objectAtIndex:0]; 
    NSString *img_path = [documentsDirectory stringByAppendingPathComponent:@"YOUR IMAGE NAME.png"]; 

    NSURL* uurl=[NSURL fileURLWithPath:img_path]; 
[thumbnailImageView setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:uurl]] forState:UIControlStateNormal]; 
+0

讓我知道如果你有一個有問題。 – user1673099

+1

爲什麼說使用NSURL它簡單[UIImage imageWithContentsOfFile:img_path]; –

0

你可以做這樣的事情獲得該文件中的圖像:

UIImage* anImage = [UIImage imageWithContentsOfFile:validFilePath]; 

,然後您可以應用圖像到這樣一個按鈕:

[_someButton setImage:anImage forState:UIControlStateNormal]; 
相關問題