从URL目标C显示图像

时间:2011-04-07 08:01:10

标签: iphone objective-c url ios4

有没有办法实现以下避免使用“initWithData”? (万一你好奇,initWithData让我的应用程序将Apple标记为使用非法A​​PI 叹息)。

NSData * imageData = [NSData dataWithContentsOfURL : [NSURL URLWithString : [details image]]];
    picture = [[UIImage alloc] initWithData:imageData];

非常感谢,

马丁

4 个答案:

答案 0 :(得分:6)

首先,您应该异步执行此操作,以便您的线程不会阻塞。这是该类的代码:

@implementation AsyncImageView

+ (void)initialize {
    [NSURLCache setSharedURLCache:[[SDURLCache alloc] initWithMemoryCapacity:0
                                                                diskCapacity:10*1024*1024
                                                                    diskPath:[SDURLCache defaultCachePath]]];
}

- (void)setImageFromURL:(NSURL *)url{
    /* Put activity indicator */
    if(!activityIndicator) {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        CGRect frame = [activityIndicator frame];
        frame.origin.x = (self.frame.size.width - frame.size.width)/2;
        frame.origin.y = (self.frame.size.height - frame.size.height)/2;
        activityIndicator.tag = 9999;
        activityIndicator.frame = frame;
        [self addSubview:activityIndicator];
        [activityIndicator startAnimating];
    }

    /* Cancel previous request */
    if(fetchImageConnection) {
        [fetchImageConnection cancel];
    }
    [imageData release];

    /* Start new request */
    NSURLRequest *req = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                     timeoutInterval:30];
    imageData = [NSMutableData new];
    fetchImageConnection = [NSURLConnection connectionWithRequest:req
                                                         delegate:self];
    [fetchImageConnection retain];
}
- (void)setImageFromDisk:(UIImage *)img {
    self.image = img;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [imageData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if(connection == fetchImageConnection) {
        self.image = [UIImage imageWithData:imageData];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"imageDownloaded" object:self];

        [activityIndicator removeFromSuperview];

        [imageData release];
        [activityIndicator release];

        activityIndicator = nil;
        imageData = nil;
        fetchImageConnection = nil;
    }
    [connection release];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];
    NSLog(@"error: %@", error);
}
@end

答案 1 :(得分:4)

如果要获取图像数据,则使用该数据初始化UIImage:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://Serverurl/pic007.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];

答案 2 :(得分:1)

NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://Serverurl/pic007.jpg"]];
 self.image=nil;
 UIImage *img = [[UIImage alloc] initWithData:receivedData ];
 self.image = img;
 [img release];

我希望这段代码可以帮到你!!

答案 3 :(得分:0)

试试这段代码:

NSString *imgString = @"https://www.lockated.com/system/attachfiles/documents/000/002/489/original/ZPMHaJUSjAGnUrVuOmbqoExRMryvcySVOIkJQMivnAntvpmpYd.jpg?1501833649";

NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgString]];

accountImageView.image = [UIImage imageWithData: imageData]; // accountImageView is imageView