2011-04-07 195 views
3

有沒有什麼辦法可以避免使用「initWithData」? (以防萬一,您好奇,initWithData正在讓我的應用程序標記爲Apple使用非法A​​PI 嘆息)。顯示來自URL的圖像Objective C

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

非常感謝,

馬丁

+1

根據iOS的參考,initWithData:是不是私人的API。 http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/initWithData: – makdad 2011-04-12 09:26:56

回答

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 
+0

我是唯一一個在「 SDURLCache「? – dfdumaresq 2013-08-15 02:13:17

4

,如果你想獲得的圖像數據,然後使用該數據初始化一個UIImage:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://Serverurl/pic007.jpg"]]; 
cell.image = [UIImage imageWithData: imageData]; 
[imageData release]; 
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]; 

我希望這個代碼將會幫助你!

+0

這幾乎是他在問題中確切的代碼 - 他特別要求解決方法initWithData :. – makdad 2011-04-12 09:24:27

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 
相關問題