1

複数の画像をダウンロードしてディスクに保存する方法。私が使用している送信リクエストは以下のとおりです。

for(NSDictionary *image in [data objectForKey:@"Catalogues"])
{
    NSString *imurl =[image objectForKey:@"Image_Path"];
    NSLog(@"%@",imurl);



    NSString *urlstring =imurl;
    NSLog(@"demo %@",urlstring);
    NSURL *mailurl =[NSURL URLWithString:urlstring];
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:mailurl];
    NSOperationQueue *ques =[[NSOperationQueue alloc]init];



    [NSURLConnection sendAsynchronousRequest:request queue:ques completionHandler:^(NSURLResponse *respo, NSData *data, NSError *err) {


        UIImage *image = [UIImage imageWithData:data];
        UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 150, 150)];
        im.image = image;
        [self.view addSubview:im];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",documentsDirectory] 

複数の画像に使用できるネイティブメソッドはありますか?

4

2 に答える 2

1

このような AsyncImage クラスを実装できます

AsyncImage.h ファイル内

#import <UIKit/UIKit.h>

@interface AsyncImage : UIView
{
    NSURLConnection* connection; 
NSMutableData* data; 
UIImageView *image;
UIActivityIndicatorView *scrollingWheel;
    NSString *imgName;
}

-(void)loadImageFromString:(NSString*)url;
-(void)loadImageFromURL:(NSURL*)url;
-(void)setLocalImage:(UIImage *)localImage;

-(id) initWithFrame:(CGRect)frame;
-(NSString *)applicationDocumentsDirectory;
-(void)cancelConnection;

@end

AsyncImage.m ファイル内

#import "AsyncImage.h"

@implementation AsyncImage

-(id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
    scrollingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    float x = self.bounds.size.width/2;
    float y = self.bounds.size.height/2;
    scrollingWheel.center = CGPointMake(x, y);
    scrollingWheel.hidesWhenStopped = YES;
    [self addSubview:scrollingWheel];
    self.clipsToBounds = YES;
    }
    return self;
}

-(void)loadImageFromString:(NSString*)url
{
[scrollingWheel startAnimating];
if (connection!=nil) { 
    [connection release];
    connection = nil;
}
if (data!=nil) { 
    [data release];
    data = nil;
}
if (image != nil) {
    [image removeFromSuperview];
    image = nil;
}
    imgName = [[[url componentsSeparatedByString:@"/"] lastObject]retain];
    //    NSLog(@"imgName=%@",imgName);
    NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imgName];   
    //    NSLog(@"imagePath=%@",imagePath);
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:imagePath] == NO)
    {
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    } else {
        UIImage *img = [[UIImage alloc]initWithContentsOfFile:imagePath];
        image = [[[UIImageView alloc] initWithImage:img] autorelease];
    image.contentMode = UIViewContentModeScaleToFill;
    image.frame = self.bounds;
    [self addSubview:image];
    [scrollingWheel stopAnimating];
    }
}

-(void)setLocalImage:(UIImage *)localImage
{
    if (image != nil) {
    [image removeFromSuperview];
    image = nil;
    }
    image = [[[UIImageView alloc] initWithImage:localImage] autorelease];
image.contentMode = UIViewContentModeScaleToFill;
image.frame = self.bounds;
[self addSubview:image];
}

//for URL
-(void)loadImageFromURL:(NSURL*)url
{
[scrollingWheel startAnimating];
if (connection!=nil) { 
    [connection release];
    connection = nil;
}
if (data!=nil) { 
    [data release];
    data = nil;
}
if (image != nil) {
    [image removeFromSuperview];
    image = nil;
}
    NSString *strurl=[NSString stringWithFormat:@"%@",url];
    imgName = [[[strurl componentsSeparatedByString:@"/"] lastObject]retain];
    NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imgName];   
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:imagePath] == NO)
    {
        NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    } else {
        UIImage *img = [[UIImage alloc]initWithContentsOfFile:imagePath];
        image = [[[UIImageView alloc] initWithImage:img] autorelease];
    image.contentMode = UIViewContentModeScaleToFill;
    image.frame = self.bounds;
    [self addSubview:image];
    [scrollingWheel stopAnimating];
    }
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [data release]; 
data=nil;
    [scrollingWheel stopAnimating];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData data] retain];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)dataObj
{
[data appendData:dataObj];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)theConnection
{
[connection release];
connection=nil;
    NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imgName];
    [data writeToFile:imagePath atomically:YES];
image = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
image.contentMode = UIViewContentModeScaleToFill;
image.frame = self.bounds;
[self addSubview:image];
[data release]; 
data=nil;
[scrollingWheel stopAnimating];
}

-(void)dealloc
{
[scrollingWheel release];
    [super dealloc];
}

-(NSString *)applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}

-(void)cancelConnection
{
    if (connection !=nil) {
        [connection cancel];
        connection=nil;
    }
    if(data!=nil){
        [data release]; 
        data=nil;
    }
[scrollingWheel stopAnimating];
}

@end

そして、あなたのviewController.mで、このクラスをインポートして、このように呼び出すことができます

AsyncImage *imgBOD = [[AsyncImage alloc] initWithFrame:CGRectMake(10, 46, 70, 70)];
[imgBOD loadImageFromString:[dictData objectForKey:@"image_path"]];
[self.view addSubview:imgBOD];
于 2013-09-07T06:11:15.460 に答える