我将创建一个UIImage
,但如果我调试该方法,它将返回nil
。
我希望有人知道我必须做什么。
我的代码:
返回nil:
UIImage *image1 = imgView.image;
整个代码块:
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
更新
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}
if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}
else{
toolbar.hidden = NO;
}
imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:1)
首次将imgView.image
设置为值时,imagePickerController:didFinishPickingMediaWithInfo:
即将结束。因此,第一次调用此方法时,imgView.image
将为nil
,直到最后设置为:{/ p>
imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
答案 1 :(得分:0)
抱歉,要跳过阅读您的代码,发布适合我的内容。
- (UIImage *) getImage{
NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:@"somefile.png" ofType:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName])
{
return [UIImage imageNamed:@"somefile.png"];
}
else{
NSString *t = [self pathToImageInCache:@"somefile.png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:t]){
NSLog(@"fiel exists");
return [[UIImage alloc]initWithContentsOfFile:t];
}
else{
NSLog(@"fiel does not exists");
}
}
return nil;
}
- (NSString *) pathToImageInCache: (NSString *) imageName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@""];
NSString *URLImg = [documentsDirectory stringByAppendingPathComponent:imageName];
return URLImg;
}
答案 2 :(得分:0)
我认为你甚至在设置它之前就试图使用imgView.image属性。
使用此行设置imageView.image
imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
将该行移动到方法的开头。像这样
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}