Iphone 显示URL目标C中的图像
是否有任何方法可以避免使用“initWithData”来实现以下目标?(以防万一,initWithData将我的应用程序标记为使用非法API) 非常感谢,Iphone 显示URL目标C中的图像,iphone,objective-c,url,ios4,Iphone,Objective C,Url,Ios4,是否有任何方法可以避免使用“initWithData”来实现以下目标?(以防万一,initWithData将我的应用程序标记为使用非法API) 非常感谢, Martin如果要获取图像数据,请使用该数据初始化UIImage: NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://Serverurl/pic007.jpg"]]; cell.image = [UIImage
Martin如果要获取图像数据,请使用该数据初始化UIImage:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://Serverurl/pic007.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];
首先,您应该异步执行此操作,以便线程不会阻塞。下面是该类的代码:
@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
我希望这段代码能帮助你 请尝试以下代码:
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
这与他在问题中的代码差不多——他特别要求解决initWithData:。根据iOS参考,initWithData:不是私有API我是唯一一个对“SDURLCache”犹豫不决的人吗?URL可能在磁盘上
NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://Serverurl/pic007.jpg"]];
self.image=nil;
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
self.image = img;
[img release];
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