开发者

writeToFile always crashes with stringByAppendingPathComponent

I am sitting on this problem for hours and I am blind now. ;-)

I have this code here:

- (id) initWithImage:(UIImageView*)imageView andPath: (NSString*) path{

   [super init];
   drawImage = imageView;
   imageFilePath = [NSString stringWithString: path];
   NSLog(@"fath: %@", imageFilePath);
   return self;
}

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(drawImage.image)];   
    NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [docsDirectory stringByAppendingPath开发者_运维百科Component:@"img.data"];
    NSLog(@"path: %@", path);
    [imageData writeToFile:path atomically:YES];

This code works but when i change

[imageData writeToFile:path atomically:YES];

to

[imageData writeToFile:imageFilePath atomically:YES];

the app crashes.

But the strings look completely the same:

2011-06-01 10:38:10.178 L3T[3756:207] fath: /Users/max/Library/Application Support/iPhone Simulator/4.3/Applications/A73945CF-9FD0-46E9-A16B-9C0EFC924B0F/Documents/img.data
2011-06-01 10:38:11.864 L3T[3756:207] path: /Users/max/Library/Application Support/iPhone Simulator/4.3/Applications/A73945CF-9FD0-46E9-A16B-9C0EFC924B0F/Documents/img.data

I just don't understand it!


The code imageFilePath = [NSString stringWithString: path]; gives you an object that you do not own, hence you cannot depend on it being available later in your code. Change this to:

imageFilePath = [[NSString alloc] initWithString: path];

and you should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜