Ios 如何查找图像是否存在-解析

Ios 如何查找图像是否存在-解析,ios,parse-platform,Ios,Parse Platform,我已经成功地从parse中检索到了图像(比如说用户配置文件pic),并将其显示在ios应用程序中。但是如果文件列中没有图像,那么我想显示存储在本地(而不是解析)中的默认图像,但不成功 我的简单问题是,如何检查是否有图像?下面是简单的代码 PFFile *userImage = [object objectForKey:@"profilePic"]; __block UIImage *userProfilePic =[[UIImage alloc]init]; [userImage getDat

我已经成功地从parse中检索到了图像(比如说用户配置文件pic),并将其显示在ios应用程序中。但是如果文件列中没有图像,那么我想显示存储在本地(而不是解析)中的默认图像,但不成功

我的简单问题是,如何检查是否有图像?下面是简单的代码

PFFile *userImage = [object objectForKey:@"profilePic"]; __block UIImage *userProfilePic =[[UIImage alloc]init];

[userImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        //this block will execute only if there is image
        if(data) {
            userProfilePic = [UIImage imageWithData:data];
            userPic.image=userProfilePic;
        }

        //this block is not at all executing. This block should execute when user doesn't have their profile uploaded
        if(!data) {
            UIImage *userProfileDefaultPic = [[UIImage alloc]init];
            userProfileDefaultPic=[UIImage imageNamed:@"defaultprofile.png"];
            userPic.image=userProfileDefaultPic;
        }
    }
}];
希望你能帮助我。提前谢谢


Pradeep

您的代码已经做到了这一点。您只需要在主线程而不是背景线程上设置图像

if(!data) { [dispatch_async(dispatch_get_main_queue(), ^{
        UIImage *userProfileDefaultPic = [[UIImage alloc]init];
        userProfileDefaultPic=[UIImage imageNamed:@"defaultprofile.png"];
        userPic.image=userProfileDefaultPic;
}}];

终于找到了解决办法

(!data)
将不会执行,而是需要检查PFFile和存储PFFile的UIImage是否有image,如下所示

PFFile *userImage = [object objectForKey:@"profilePic"];
               __block UIImage *userProfilePic =[[UIImage alloc]init];
if(userImage && ![userProfilePic isEqual:[NSNull null]]) //This is important to check if the file if image is there or not and if not, then display default pic in else part

{
            [userImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if(!error)
                {

                        userProfilePic = [UIImage imageWithData:data];
                        userPic.image=userProfilePic;

                }
                 }];
            }
            else
            {
                userProfilePic =[UIImage imageNamed:@"defaultprofilepic.png"];
                userPic.image=userProfilePic;

            }

尝试记录“userPic.image”并发布结果。为这两个实例添加日志。。。图像存在,图像不存在。谢谢Jeet。我没试过这个,我会的try@jeet.chanchawat,尝试登录(userPic.image)时出错。我已经做了一些其他的方式-上传默认图像,而用户注册。谢谢你试着帮助我。我需要查一下Rahmi的最新代码谢谢Rahim。但是默认图像没有显示,我尝试了不同的方法,没有成功。您需要在主线程中更新图像。[dispatch_async(dispatch_get_main_queue(),^{///在此处更新并设置映像/}];请参阅编辑的答案。不幸的是,它不起作用。我尝试了sysc和async