如何从iphone文件夹中设置按钮背景

时间:2013-10-28 05:37:57

标签: ios iphone uiimageview uibutton uiimage

我有图像文件存储在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];

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

您可以通过执行以下操作从文件中获取图像:

UIImage* anImage = [UIImage imageWithContentsOfFile:validFilePath];

然后您可以将该图像应用于这样的按钮:

[_someButton setImage:anImage forState:UIControlStateNormal];