【问题标题】:how to load image to uiimageview如何将图像加载到uiimageview
【发布时间】:2014-08-15 20:56:09
【问题描述】:

如何将图像从自定义文件夹加载到 uiimageview

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString*ext= [NSString stringWithFormat:@"/Imagenes/30775.png"];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:ext];
     //option 1 
     UIImage * myImage = [UIImage imageWithContentsOfFile: dataPath];
    _img.image = myImage
    ///option 2
    UIImageView * myImageView = [[UIImageView alloc] initWithImage: myImage];
    _img = myImageView;
    ///option 3
    NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dataPath]];
    _img.image = [UIImage imageWithData:imgData];

我尝试了 3 个选项,但都不起作用 有什么想法吗?

图片正在从自定义文件夹加载 我检查了路径,是正确的,但没有加载

【问题讨论】:

    标签: ios image uiimage load


    【解决方案1】:

    您的路径 @"/Imagenes/30775.png" 似乎是错误的。仔细检查一下。否则以下应该工作

     UIImage * myImage = [UIImage imageWithContentsOfFile: dataPath];
    _img.image = myImage
    

    【讨论】:

      【解决方案2】:
       NSString*ext= [NSString stringWithFormat:@"/Imagenes/30775.png"];
      

      您的路径错误。我猜 Imagenes 是您项目中的一个组。所以 xcode 不会在您的项目主文件夹中创建文件夹。这些组仅帮助您在 xcode 程序中轻松查看文件。 你也不需要stringWithFormat

      试试这个;

       NSString*ext= @"30775.png";
      

      【讨论】:

        猜你喜欢
        • 2020-10-02
        • 1970-01-01
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        相关资源
        最近更新 更多