0

我从我的网站下载的图片有问题。

我的CollectionView显示图像已经添加到我的项目中,但是当我从Pictures.plist文件信息下载新图像时。我一无所有。

我在下载之前检查图像是否已经下载。但是……我不知道。

这是我的代码:

 #pragma mark - Téléchargement d'un fichier image mis à jour
- (void)downloadImageFileIfUpdated:(NSString *)file withPath:(NSString *)urlPath {

    //NSLog(@"[downloadImageFileIfUpdated withPath]");
    BOOL downloadFromServer = NO;

    /// Définition de l'adresse du fichier serveur
    NSURL *url = [NSURL URLWithString:[urlPath stringByAppendingString:file]];
    //NSLog(@"Téléchargement de l'entête HTTP depuis : %@", url);

    /// Récupération du fichier local
    NSArray *picturesInfosPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *picturesInfosDirectoryPath = [picturesInfosPath objectAtIndex:0];
    NSString *cachedPath = [picturesInfosDirectoryPath stringByAppendingPathComponent:file];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *lastModifiedString = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"HEAD"];
    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
    if([response respondsToSelector:@selector(allHeaderFields)]) {
        lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];
    }

    NSDate *lastModifiedServer = nil;
    @try {
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
        df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        lastModifiedServer = [df dateFromString:lastModifiedString];
    }
    @catch(NSException * e) {
        NSLog(@"Error parsing last modified date : %@ - %@", lastModifiedString, [e description]);
    }
    //NSLog(@"Dernière modification du fichier serveur : %@", lastModifiedServer);

    NSDate *lastModifiedLocal = nil;
    if([fileManager fileExistsAtPath:cachedPath]) {
        NSError *error = nil;
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:cachedPath error:&error];
        if (error) {
            NSLog(@"Error reading file attributes for : %@ - %@", cachedPath, [error localizedDescription]);
        }
        lastModifiedLocal = [fileAttributes fileModificationDate];
        //NSLog(@"Dernière modification du fichier local : %@", lastModifiedLocal);
    }

    /// Activation du téléchargement si on a pas de fichier local
    if(!lastModifiedLocal) {
        downloadFromServer = YES;
    }

    /// Activation du téléchargment si le fichier serveur Pictures.plist possède une mise à jour
    if([lastModifiedLocal laterDate:lastModifiedServer] == lastModifiedServer) {
        downloadFromServer = YES;
    }

    /// Téléchargement du fichier serveur
    if(downloadFromServer) {
        //NSLog(@"Télécharment du nouveau fichier serveur");
        NSData *data = [NSData dataWithContentsOfURL:url];
        if(data) {
            /// Enregistrement du fichier
            if([data writeToFile:cachedPath atomically:YES]) {
                //NSLog(@"Téléchargement du fichier sauvé à : %@.", cachedPath);
            }
            /// Déclaration de la date de modification du fichier local en accord avec celle du fichier du serveur
            if(lastModifiedServer) {
                NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate];
                NSError *error = nil;
                if([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) {
                    //NSLog(@"File modification date updated.");
                }
                if(error) {
                   // NSLog(@"Error setting file attributes for: %@ - %@.", cachedPath, [error localizedDescription]);
                }
            }
        }
    }
}


#pragma mark - Initialisation des informations
-(void) initializeViewWithFile:(NSString *)file {

    NSLog(@"[initializeViewWithFile]");
    /// Arrêt de l'indication de chargement de la vue
    [loadingView stopAnimating];

    NSString *fileName = [file stringByDeletingPathExtension];
    NSString *fileExtension = [file pathExtension];
    NSString *fileLocalPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    fileLocalPath = [rootPath stringByAppendingPathComponent:file];
    if(![[NSFileManager defaultManager] fileExistsAtPath:fileLocalPath]) {
        fileLocalPath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileExtension];
    }
    NSDictionary* dictFromFile = [[NSDictionary alloc] initWithContentsOfFile:fileLocalPath];
    NSMutableArray* arrayFromFile = [dictFromFile objectForKey:@"Root"];

    /// Création d'un tableau qui contient les objets images
    gallery = [[NSMutableArray alloc] init];
    NSEnumerator *enumerator = [arrayFromFile objectEnumerator];
    NSDictionary *obj;
    while((obj = [enumerator nextObject])) {
        PMPicture *picture = [[PMPicture alloc] initWithDictionaryFromPlist: obj];
        [gallery addObject: picture];
        [self downloadImageFileIfUpdated:picture.file withPath:picture.url];
    }

    /// Initialisation
    [galleryCollectionView setDataSource:self];
    [galleryCollectionView setDelegate:self];
    [galleryCollectionView reloadData];
}

集合视图方法:

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

 return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:   (NSInteger)section {

 return gallery.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"GalleryCell";
PMPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
PMPicture *picture = [gallery objectAtIndex:indexPath.item];

// UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100];
// cellImageView.image = [UIImage imageNamed:[picture file]];

NSString *image = [picture file]; // NOT WORKING, BUT IS LIKE @"img23.png"
//NSString *image = @"radios.png"; // WORK, I THINK BECAUSE IS ADDED IN PROJECT
//NSLog(@"Image affichée : %@", image);
[[cell cellImage] setImage:[UIImage imageNamed:image]];
return cell;
}

就是这样,如果你能帮助我,我将不胜感激!

我的观点开始于initializeViewWithFile:file,文件在哪里myPictures.plist

谢谢

4

1 回答 1

0
NSString *ImagePath = @"Full Path of Image";
[img setImage:[UIImage imageWithContentsOfFile:ImagePath] forState:UIControlStateNormal];
  1. 在这里,我给出了对我有用的示例..在 ImageView 中。谢谢。
于 2013-04-09T10:03:58.767 回答