问题描述
|
我正在尝试在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]];