2013-03-26 38 views
0

我已經在文件目錄與下面的代碼,並將其圖像後保存的圖像會出現在紀錄片目錄成功顯示圖像,其保存在文件目錄

UIImage *image =[[UIImage alloc ]init ]; 
image = [UIImage imageNamed:@"PlaceHolder.png"]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: @"PlaceHolder.png"] ]; 
    NSData* data = UIImagePNGRepresentation(image); 
    [data writeToFile:path atomically:YES]; 

現在我想顯示在表視圖圖像 在我的表視圖某些圖像越來越爲什麼我用下面的代碼從Facebook網址這就是顯示

NSString *path = [[_arrayForTable valueForKey:@"URL"] objectAtIndex:[indexPath row]]; 

NSURL *url = [NSURL URLWithString:path]; 
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease]; 
imageView.frame = CGRectMake(0, -5, 45, 45); 
imageView.imageURL=url; 

_arrayForTable是迪科的陣列,它包括URL鏈接一些來自Facebook和一些來自documen牛逼目錄

現在我的問題是如下

**僅Facebook的圖片越來越顯示在表視圖,但文件沒有得到顯示目錄圖像**

我已經檢查的位置是圖像完全正確但現在顯示

+0

從文檔目錄中放置圖像顯示代碼.. – 2013-03-26 07:49:29

+0

我從來沒有使用過AsyncImageView類,但從我的經驗來看,應該使用[[UIImage alloc] initWithContentsOfFile:filePath]加載在目錄中找到的圖像。 – Cosmin 2013-03-26 07:49:58

+0

但有些圖像正從url顯示,所以如何做到這一點是從網址和一些從uiimage? – user2189388 2013-03-26 07:51:43

回答

1
NSURL *url = [NSURL URLWithString:path]; 

這是錯誤的代碼。用這行代碼替換它。存在於你的文檔目錄

NSURL *url = [NSURL fileURLWithPath:path]; 

檢查文件是否

if([[NSFileManager defaultManager] fileExistsAtPath:path]) 
    NSURL *url = [NSURL fileURLWithPath:path]; 

而且在你的文檔目錄檢查,如果圖像是越來越保存與否。您將得到圖像的名稱這樣

image = [UIImage imageNamed:@"PlaceHolder.png"]; 

所以如果你重複這條線的圖像將會在文檔目錄被覆蓋。

你也可以在這裏輸入一個支票 BOOL success = [data writeToFile:path atomically:YES];

if(success) 
    NSLog(@"image written successfully"); 
else 
    NSLog(@"image writing failed"); 

希望我幫了忙。

0

你檢查圖像數據不爲零的文件目錄的形象呢?你必須通過檢索正確的路徑圖像,並確保你給正確的位置

+0

我已經在那裏檢查過。有圖像和是路徑是完全正確的 – user2189388 2013-03-26 07:52:51

0

AsyncImageViewUIImageView的一個子類。在使用initWithContentsOfFile:filePath加載圖像後,您可以簡單地設置image屬性。你只需要弄清楚你想從目錄加載哪些圖像,以及哪些圖像來自網絡。

0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [[_arrayForTable valueForKey:@"URL"] objectAtIndex:[indexPath row]]; 

NSURL *url = [NSURL URLWithString:path]; 
if ([path rangeOfString:documentsDirectory].location != NSNotFound) 
    url = [NSURL fileURLWithPath:path isDirectory:NO]; 

AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease]; 
imageView.frame = CGRectMake(0, -5, 45, 45); 
imageView.imageURL=url; 
0

Asynchimageview是否支持從文檔目錄加載圖像或與圖像URL綁定??嘗試從包首先加載圖像。最近我有類似的問題,並最終從服務器本身加載佔位符。

像在其他一些答案中,您可以首先創建一個UIImage對象與initWithContentsOfFile:asynchimageView.image

1

在你的。M檔

@interface YourClass()<NSURLConnectionDelegate> 
@property (nonatomic, copy) NSMutableData *receivedImageData; 
@end 

@implementation YourClass 

- (void)init { 
    if (self = [super init]) { 
     // Do your init stuff 
     _receivedImageData = [[NSMutableData alloc] initWithCapacity:0]; 
    } 
} 

// A method to start your connection 
- (void)startDownloadingImage { 
    NSMutableURLRequest *downloadImageRequest = [[NSMutableURLRequest alloc] init]; 
    downloadImageRequest.URL = [NSURL URLWithString:@"http://www.theImage.you/want/to/download.extension"]; 
    downloadImageRequest.HTTPMethod = @"GET"; 
    [NSURLConnection connectionWithRequest:downloadImageRequest delegate:self]; 

} 

// You need the following delegate methods 
#pragma mark - 
#pragma mark NSURLConnection delegate methods 

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse { 
    return request; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    // This method is called frequently ... you can see if you log 
    // NSLog(@"I am downloading"); 
    [self.receivedImageData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    // Log an error or do whatever you want ;) 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // Create your picture here using 
    UIImage *image = [UIImage imageWithData:self.receivedImageData scale:1.0]; 
    // Only if there is an image display the downloaded image 
    if (CGSizeEqualToSize(self.imageView.image.size, CGSizeZero)) { 
     self.imageView.image = image; 
    } 

}

你可以嘗試下載圖像,如果沒有圖像,你可以持續顯示PlaceHolder.png 確保您複製的形象和它確實是在項目目錄

// self.imageView is an UIImageView 
self.imageView.image = [UIImage imageNamed:@"PlaceHolder.png"]; 
[self startDownloadingImage]; 

讓我知道,如果它幫助了你;)

相關問題