gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:40:50 25 4
gpt4 key购买 nike

我已经成功地从解析中检索到图像(比如说用户个人资料图片),并且我正在 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;
}
}
}];

希望你能帮助我。提前致谢。

普拉迪普

最佳答案

终于找到解决办法了!

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

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;

}

关于ios - 如何查找图像是否存在 - 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com