使用iPhone sdk从URL在图像视图中加载图像?

使用iPhone sdk从URL在图像视图中加载图像?,iphone,objective-c,image,Iphone,Objective C,Image,为了通过URL加载图像视图,我已经从web上浏览了很多代码。不幸的是,我尝试过的解决方案没有一个对我有效,而且我会出现非常奇怪的错误。我正在获取图像数据,但此数据中没有图像。我将下面的代码与一个示例URL一起使用,它可以工作,给我呈现的图像。然而,当我切换到使用我的实际URL时,我遇到了问题,因为它似乎试图创建/返回一个太大的图像。这是我的密码: NSString *urlString = [[[_xmlDictionary objectForKey:@"response"] objec

为了通过URL加载图像视图,我已经从web上浏览了很多代码。不幸的是,我尝试过的解决方案没有一个对我有效,而且我会出现非常奇怪的错误。我正在获取图像数据,但此数据中没有图像。我将下面的代码与一个示例URL一起使用,它可以工作,给我呈现的图像。然而,当我切换到使用我的实际URL时,我遇到了问题,因为它似乎试图创建/返回一个太大的图像。这是我的密码:

    NSString *urlString = [[[_xmlDictionary objectForKey:@"response"] objectForKey:@"movie"] objectForKey:@"poster"];   

    NSArray *parts = [urlString componentsSeparatedByString:@"\""];
    urlString = [parts objectAtIndex:1];

    urlString = [urlString stringByAppendingFormat:@"/"];
    urlString = [urlString  
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //NSURL * imageURL = [NSURL URLWithString:urlString];
    NSLog(@"URL:%@",urlString);
    NSURL * imageURL = [NSURL URLWithString:urlString];
    NSData * imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
    UIImage * image = [[UIImage alloc] initWithData:imageData];
    m_imageView = [[UIImageView alloc] initWithImage:image];
    m_imageView.frame = CGRectMake(10, 10,300,300);
    [self.view addSubview:m_imageView];
    [imageData release];
    [image release];

我非常感谢您的帮助,谢谢。

< P>对于大量数据,您应该考虑使用NSURLCONTION:

NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];

NSURLConnection * conn = [NSURLConnection connectionWithRequest:request delegate:myDelegate];
这样,您就可以通过以下方式跟踪从服务器返回的数据:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
您已经声明,
我在下面使用了具有不同url的代码,它给出了图像
,如果您提供了您尝试使用的url,这将非常有用


帮助追踪问题的另一个方法是UIWebView验证您是否能够加载所需的图像。

您可以将URL中的响应转换为NSData,然后使用

NSURL *url = [NSURL URLWithString:@"<YOUR URL STRING HERE>"];

NSData *data = [[NSData alloc] initWithContentsOfURL:url];

UIImage *tmpImage = [[UIImage alloc] initWithData:data];

yourImageView.image = tmpImage;

请尝试以下代码。我已经测试过了。工作正常

//in .h file

 IBOutlet UIImageView *imgTest;

-(IBAction)buttonTapped:(id)sender;
-(void)LoadImage:(NSString *) irlString;
-(void)setImage:(NSData *) imgData;

//in .m file write the following code:

-(IBAction)buttonTapped:(id)sender
{
    [self performSelectorOnMainThread:@selector(LoadImage:) withObject:@"http://www.google.com/images/errors/logo_sm.gif" waitUntilDone:NO];
}

-(void)LoadImage:(NSString *) urlString
{
    NSURL *imgURL=[NSURL URLWithString:urlString];
    NSData *imgData=[NSData dataWithContentsOfURL:imgURL];
    [self performSelectorInBackground:@selector(setImage:) withObject:imgData];
}

-(void)setImage:(NSData *) imgData;
{
    imgTest.image=[UIImage imageWithData:imgData];
}

这将帮助您从网站上获取图像、段落和标题

如果你想让我帮你或者得到第二张图片或者任何你需要的帮助,我会重新编写我的代码

这是一个相当晚的答案,但我认为它仍然可能有助于你的未来。您可以对网站进行解析,这是正确的方法,但我将向您展示如何以不同的方式进行解析,这也可以用于读取xml、html、.com、任何内容以及.rss,以便它可以读取rss提要。
在这里:

这可以得到你的第一段,如果你要求我会告诉你如何得到第二段等等。因此,如果您想获得金钱,请更改扫描仪以查找
$
标题将是名称,但如果不是,请告诉我,我将向您展示如何获取第二个标题或标题。看看这个和下面。我将向你展示如何从整个网站中获取html或xml,这样你就可以看到网站的全部代码,并查找关于的内容。因此,请先阅读网站,然后阅读代码,然后在代码中看到狗的名字,找到标题标签或标题下的内容,并根据需要更改下面的代码阅读整个文章以了解内容一切。下面是如何获取信息的主要代码。转到第二个代码块,了解如何在日志或控制台中获取网站代码。要找到与您的图像相同的内容,请查看NSLog,并查找任何图像链接,如www.example.com/images/image.jpg或image.png或image.gif或一些图像扩展,然后查看是什么代码启动了这一过程,比如html和xml中有100个图像代码,查找url查看代码,然后在扫描仪中使用它将更改为您的代码,然后确保只有图像的url仍然存在,就像不断缩短它,直到您除了url之外什么都没有

你有吗

您需要:www.example.com/images/image.jpg


使用下面的扫描仪,将

改为<代码>这里有另一个堆栈溢出解决方案:谢谢BrayMAC,但是我不知道我得到数据的原因是什么,但是图像变量显示NILL,这个代码与其他图像URL一起工作。请考虑包含一些关于您的答案的信息,而不是简单地发布代码。我们努力提供的不仅仅是“修复”,而是帮助人们学习。您应该解释原始代码中的错误,您的不同做法,以及您的更改起作用的原因;在主线程上。例如,dispatch_async(dispatch_get_main_queue(),^{yourmageview.image=tmpImage;});为什么要在主线程上下载图像数据并在后台线程上更新UI?难道不是相反吗?

//in .h file

 IBOutlet UIImageView *imgTest;

-(IBAction)buttonTapped:(id)sender;
-(void)LoadImage:(NSString *) irlString;
-(void)setImage:(NSData *) imgData;

//in .m file write the following code:

-(IBAction)buttonTapped:(id)sender
{
    [self performSelectorOnMainThread:@selector(LoadImage:) withObject:@"http://www.google.com/images/errors/logo_sm.gif" waitUntilDone:NO];
}

-(void)LoadImage:(NSString *) urlString
{
    NSURL *imgURL=[NSURL URLWithString:urlString];
    NSData *imgData=[NSData dataWithContentsOfURL:imgURL];
    [self performSelectorInBackground:@selector(setImage:) withObject:imgData];
}

-(void)setImage:(NSData *) imgData;
{
    imgTest.image=[UIImage imageWithData:imgData];
}
        NSString *img=[NSString stringWithFormat:@"https://i1.sndcdn.com/avatars-000098290475-iw9m74-large.jpg?e76cf77"];
        NSURL *url = [NSURL URLWithString:img];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage *img1 = [[UIImage alloc]initWithData:data];
        self.artWorksImage.image = img1;
        NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];



        NSString *content2222 = [[NSString alloc] init];




        //Change <p> to suit your need like <description> or <h1>
        [stringScanner2222 scanUpToString:@"<p>" intoString:Nil];



        [stringScanner2222 scanUpToString:@"." intoString:&content2222];







        NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];

        description.text = filteredTitle;
[stringScanner2222 scanUpToString:@"<media:content url:\"" intoString:Nil];
                [stringScanner2222 scanUpToString:@"\">" intoString:&content2222];
NSURL * imageURL = [NSURL URLWithString:[filteredTitle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]])];

        UIImage * image = [UIImage imageWithData:imageData];

        [imageView setImage:image];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;



//Do this with your textview

NSString *webStringz = description.text;



// Leave this
NSString *mastaString;
mastaString = webStringz;
{
    NSString *webString2222 = mastaString;



    NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];



    NSString *content2222 = [[NSString alloc] init];




    //Change <p> to suit your need like <description> or <h1>
    [stringScanner2222 scanUpToString:@"<p>" intoString:Nil];



    [stringScanner2222 scanUpToString:@"." intoString:&content2222];







    NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];

    description.text = filteredTitle;


}
//This is your URL
        NSURL *URL = [NSURL URLWithString:@"URL HERE"];
        //This is the data your pulling (dont change)
        NSData *data = [NSData dataWithContentsOfURL:URL];

        // Assuming data is in UTF8. (dont change)
        NSString *string = [NSString stringWithUTF8String:[data bytes]];
        //Your textView your not done.
        description.text = string;
        NLog(@"%@", string)


        //Do this with your textview

        NSString *webStringz = description.text;



        // Leave this
        NSString *mastaString;
        mastaString = webStringz;