微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIImage imageNamed

如何解决UIImage imageNamed

| 我正在尝试在OpenFlow API中将URL用作“ 0”。
Nsstring *imageUrl = [[[newsEntries objectAtIndex:0] objectForKey: @\"still\"] retain];
NSURL *url2 = [NSURL URLWithString:imageUrl];
NSData *photoData = [NSData dataWithContentsOfURL:url2];
UIImage *imageUI = [UIImage imageWithData:photoData]
UIImageView *myImage = [UIImageView initWithFrame:imageUI];
[(AFOpenFlowView *)self.view setimage:[UIImage imageNamed:myImage]];
[imageUr release];

[(AFOpenFlowView *)self.view setNumberOfImages:3]; 
我已经尝试过了,但是没有成功。我使该API正常工作的唯一方法是使用
imageNamed
类型。
initwithData
没有成功。 那么,如何更改此
Nsstring
最终成为
imageNamed
方法?     

解决方法

UIImageView与UIImage不同。 更改此行:
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:myImage]];
为此:
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageUI]];
它应该工作。     ,您在这里有几个重大错误。看来您可能需要阅读有关C / Objective-C和类型的信息。 听起来您好像在断言那条线
UIImage *imageUI = [UIImage imageWithData:photoData]
不管用。到目前为止的代码看起来还不错(尽管没有必要保留然后释放
imageUrl
变量。 创建
UIImage
之后,您应该可以将其直接传递给
AFOpenFlowView
[(AFOpenFlowView*)self.view setImage:imageUI forIndex:0];
线
UIImageView *myImage = [UIImageView initWithFrame:imageUI];
有两个错误(无论如何都是不必要的)。首先,
-initWithFrame
CGRect
作为自变量,而不是
UIImage
UIImageView
确实具有初始化方法
-initWithImage
,这可能是您想要的。但是无论哪种方式,以\“ init \”开头的方法都是实例方法,而不是类方法,因此您必须在
UIImageView
的实际实例上调用它们,如下所示:
UIImageView *myImage = [[UIImageView alloc] initWithImage:imageUI];
请注意,这将导致内存泄漏,除非您自动释放或释放它。 在下面的行中,您正确地尝试给
AFOpenFlowView
赋予
UIImage
,但是您尝试通过将
UIImageView
传递给
+imageNamed
方法来创建
UIImage
+imageNamed
接受一个包含图像名称的NSString,将其他任何内容传递给它均无效。 您必须始终了解方法期望接收的对象类型,并确保找到某种方法来实现该目的。 大概您在这里寻找的是这样的:
NSString *imageUrl = [[newsEntries objectAtIndex:0] objectForKey: @\"still\"];
NSString *imageName = [imageUrl lastPathComponent];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName]];
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。