0

Jsonの配列URL画像があります。

アプリ内で画像をダウンロード、保存、表示したい。アプリに画像が存在することを確認してください。

手伝って頂けますか!

少し早いですがお礼を

コードのダウンロード、保存、表示:->チェック画像が存在するかどうかわかりません使用:

for (int i=0; i<[self.imageURLs count]; i++) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"img%d.jpg",i]]) {
            NSLog(@"file exists at the path");
        }
        else
            NSLog(@"file doesnt exist");

        int y_lblLogo = i==0 ? 45 : (20 * (i+1)) - 15 ;
        UIImage * imageFromURL = [self getImageFromURL:[self.imageURLs objectAtIndex:i]];

        [self saveImage:imageFromURL withFileName:[NSString stringWithFormat:@"img%d",i] ofType:@"png" inDirectory:documentsDirectoryPath];

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(200 * i, 0, 100, 100)];
        UIImage * imageFromWeb = [self loadImage:[NSString stringWithFormat:@"img%d",i] ofType:@"png" inDirectory:documentsDirectoryPath];
        imgView.image = imageFromWeb;

        [imgView drawRect:CGRectMake(0,y_lblLogo,70.f,30.f)];
        //[self.view addSubview:imgView];
    }

    -(UIImage *) getImageFromURL:(NSString *)fileURL {
              UIImage * result;
             NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
            result = [UIImage imageWithData:data];
             return result; 
    }

    -(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
             if ([[extension lowercaseString] isEqualToString:@"png"]) {
                [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",
          imageName, @"png"]] options:NSAtomicWrite error:nil];
              } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
                 [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString
          stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite
         error:nil];
             } else {

            } 
    }

     -(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
              UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];
              return result; 
}
4

1 に答える 1

1

必要な完全なコードは次のとおりです。

- (UIImage *)imageFromURL:(NSString*) surl fromCache:(BOOL)fromCache
{
UIImage *img;
if(surl!=nil && [surl compare:@""]!=NSOrderedSame) {
    NSString* key = [self getFilenameFromUrl:surl];
    NSData *data = [self getDataFromURL:surl fromCache:fromCache];
    if(data) {
        img = [[UIImage alloc] initWithData:data];
    }
}
return img;
}

-(NSString*) getFilenameFromUrl:(NSString*) surl
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^a-zA-Z0-9\\._]"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];
return [regex stringByReplacingMatchesInString:surl options:0 range:NSMakeRange(0, [surl length]) withTemplate:@"_"];
}

-(NSString*) getCacheFolder {
    NSFileManager *filemgr = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sDocumentsDir = [documentPaths objectAtIndex:0];
    sDocumentsDir = [sDocumentsDir stringByAppendingPathComponent:@"/.tmp"];
    BOOL isDir;
    if(!([filemgr fileExistsAtPath:sDocumentsDir isDirectory:&isDir] && isDir))
    {
        [filemgr createDirectoryAtPath:sDocumentsDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return sDocumentsDir;
}

-(NSData *) getDataFromURL:(NSString*) surl fromCache:(BOOL) fromCache
{
    NSData *urlData;

    if(surl!=nil && [surl compare:@""]!=NSOrderedSame) {
        surl = [surl stringByReplacingOccurrencesOfString:@" " withString:@"20"];
        NSFileManager *filemgr = [NSFileManager defaultManager];
        NSString *furl = [NSString stringWithFormat:@"%@/%@",[self getCacheFolder],[self getFilenameFromUrl:surl]];

        if (fromCache && [filemgr fileExistsAtPath: furl]==YES) {
            //NSLog (@"File exists");
            urlData = [filemgr contentsAtPath: furl];
        } else if([self hasConnection]){
            //NSLog (@"File not found");
            [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
            NSURL  *url = [NSURL URLWithString:surl];
            NSError *err;
            urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&err];
            if (!err && urlData)
            {
                [filemgr createFileAtPath: furl contents: urlData attributes: nil];
            } else {
                NSLog(@"%@",err);
            }
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        }
    }

    return urlData;
}

また、たとえばFGalleryなどの既存のコンポーネントを使用して表示することもできます。

于 2012-10-30T10:09:57.057 に答える