Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获取uiimage的原始nsdata?_Ios_Uiimage_Nsdata - Fatal编程技术网

Ios 如何获取uiimage的原始nsdata?

Ios 如何获取uiimage的原始nsdata?,ios,uiimage,nsdata,Ios,Uiimage,Nsdata,在我的应用程序中,用户可以从相册或相机中选择一张图片,在那里我可以获得图像表示。因为相册可能有来自web的pic,所以文件类型不仅仅是jpg。然后我需要将其发送到服务器而不进行转换。在这里,我只能使用nsdata。 我知道UIImageJPEGRepresentation和UIImagePNGRepresentation,但我认为这两种方法可以转换原始图像。也许当质量设置为1时,UIImageJPEG表示可以获得原始图片? 是否有任何方法可以获取原始uiimage nsdata?您可以使用A

在我的应用程序中,用户可以从相册或相机中选择一张图片,在那里我可以获得图像表示。因为相册可能有来自web的pic,所以文件类型不仅仅是jpg。然后我需要将其发送到服务器而不进行转换。在这里,我只能使用nsdata。
我知道UIImageJPEGRepresentation和UIImagePNGRepresentation,但我认为这两种方法可以转换原始图像。也许当质量设置为1时,UIImageJPEG表示可以获得原始图片?

是否有任何方法可以获取原始uiimage nsdata?

您可以使用
AlassetLibrary
ALAssetRepresentation
获取原始数据。例如:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:imageURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *repr = [asset defaultRepresentation];
        NSUInteger size = repr.size;
        NSMutableData *data = [NSMutableData dataWithLength:size];
        NSError *error;
        [repr getBytes:data.mutableBytes fromOffset:0 length:size error:&error];
            /* Now data contains the image data, if no error occurred */
    } failureBlock:^(NSError *error) {
        /* handle error */
    }];
}
但有一些事情需要考虑:

  • assetForURL:
    异步工作
  • 在设备上,使用
    assetForURL:
    将导致确认对话框,这可能会激怒用户:
“您的应用程序”希望使用您的当前位置。这允许访问 在照片和视频中查看位置信息

  • 如果用户拒绝访问,
    assetForURL:
    调用故障块
  • 下次使用此方法时,
    assetForURL:
    将失败,无需再次询问用户。只有在系统设置中重置位置警告时,才会再次询问用户

因此,您应该准备好该方法失败,并使用
uiimagejpegresentation
UIImagePNGRepresentation
作为备用方案。但在这种情况下,您将无法获取原始数据,例如元数据(EXIF等)丢失。

您可以使用
AlassetLibrary
ALAssetRepresentation
获取原始数据。例如:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:imageURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *repr = [asset defaultRepresentation];
        NSUInteger size = repr.size;
        NSMutableData *data = [NSMutableData dataWithLength:size];
        NSError *error;
        [repr getBytes:data.mutableBytes fromOffset:0 length:size error:&error];
            /* Now data contains the image data, if no error occurred */
    } failureBlock:^(NSError *error) {
        /* handle error */
    }];
}
但有一些事情需要考虑:

  • assetForURL:
    异步工作
  • 在设备上,使用
    assetForURL:
    将导致确认对话框,这可能会激怒用户:
“您的应用程序”希望使用您的当前位置。这允许访问 在照片和视频中查看位置信息

  • 如果用户拒绝访问,
    assetForURL:
    调用故障块
  • 下次使用此方法时,
    assetForURL:
    将失败,无需再次询问用户。只有在系统设置中重置位置警告时,才会再次询问用户

因此,您应该准备好该方法失败,并使用
uiimagejpegresentation
UIImagePNGRepresentation
作为备用方案。但在这种情况下,您将无法获得原始数据,例如元数据(EXIF等)丢失。

在iOS 8.0+上,在资产中找到相应的资产后,使用PHImageManager.default().requestImageData()(您可以使用PHAsset.fetchAssets()访问资产)


请参阅非常类似问题中的更多信息和示例代码。

在iOS 8.0+上,在资产中找到相应的资产后,使用PHImageManager.default().requestImageData()(可以使用PHAsset.fetchAssets()访问资产)


查看“非常类似的问题”中的更多信息和示例代码。

据我所知,相机卷中的图片是以JPEG格式存储的(至少这是我在关闭设备并检查文件后看到的格式)。因此,JPEG表示法在您的case@KaanDedeoglu:许多应用程序都有“存储在相册中”的功能,这也适用于TIFF、BMP和其他一些文件类型。据我所知,相机卷中的图片是以JPEG格式存储的(至少这是我在下载设备并检查文件后看到的格式)。因此,JPEG表示法在您的case@KaanDedeoglu:许多应用程序都有“存储在相册中”的功能,这也适用于TIFF、BMP和其他一些文件类型。谢谢!我还没有注意到imagePickerController:didFinishPickingImage:editingInfo:已被弃用。谢谢!我还没有注意到imagePickerController:didFinishPickingImage:editingInfo:已被弃用。