gpt4 book ai didi

ios - UIImage 返回 nil

转载 作者:行者123 更新时间:2023-11-29 03:00:16 25 4
gpt4 key购买 nike

我将创建一个UIImage,但如果我调试该方法,它会返回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];
}

最佳答案

第一次将 imgView.image 设置为值是在 imagePickerController:didFinishPickingMediaWithInfo: 末尾附近。因此,第一次调用此方法时,imgView.image 将为 nil,直到最后设置为:

imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

关于ios - UIImage 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23391956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com