NSData PNG无效的PNG文件:iDOT没有指向有效的IDAT块

时间:2014-05-23 21:41:00

标签: ios objective-c uiimage nsmutabledata

每当我尝试在App中打开图像时,都会出现以下错误:

:ImageIO:PNG无效的PNG文件:iDOT未指向有效的IDAT块

:ImageIO:PNG无效的PNG文件:iDOT未指向有效的IDAT块

:ImageIO:PNG无效的PNG文件:iDOT未指向有效的IDAT块

这是我的头文件:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <dispatch/dispatch.h>


@interface ImageViewController : UIViewController<NSURLConnectionDataDelegate>{
    dispatch_queue_t background;
}

@property (nonatomic,strong) PFObject *message;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic,strong) NSMutableData *imageData;
@property (nonatomic,strong) NSURLConnection *connection;
- (IBAction)save:(id)sender;

@end

以下是实施:

#import "ImageViewController.h"

@interface ImageViewController ()

@end

@implementation ImageViewController



- (void)viewDidLoad
{
    NSLog(@"ImageViewController starts here");
    [super viewDidLoad];

        PFFile *imageFile = [self.message objectForKey:@"imageFile"];
        NSURL *imageURL = [[NSURL alloc]initWithString:imageFile.url];
    NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:imageURL];
    self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
    [self.connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    [self.connection start];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (self.imageData == nil){
        self.imageData = [[NSMutableData alloc]init];
        [self.imageData appendData:data];
    }


}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.imageView.image = [UIImage imageWithData:self.imageData];

}

@end

1 个答案:

答案 0 :(得分:5)

你现在实际上并没有将除了初始块之外的任何内容附加到imageData对象,因此损坏了PNG。

更改此

if (self.imageData == nil){
    self.imageData = [[NSMutableData alloc]init];
    [self.imageData appendData:data];
}

到此

if (self.imageData == nil){
    self.imageData = [[NSMutableData alloc]init];
}
[self.imageData appendData:data]; // <-- moved outside conditional