我的代码目前正在使用什么:
我从照片库中选择JPG或PNG(使用标准的ImagePicker方法),并使用以下方法将该图像转换为NSData:
self.myImageData = UIImageJPEGRepresentation(myImage, 0.9);
然后我使用multipart / form-data发布到服务器。
我现在想要为GIF做同样的事情,同时保留原始的GIF数据(以便动画GIF进入图书馆,然后恢复出来仍然是动画效果)。
在didFinishPickingMediaWithInfo中,我可以使用
获取原始GIF的URLself.myGIFURL = [info objectForKey:UIImagePickerControllerReferenceURL].
以下是可能让我得到的一个例子:
资产库://asset/asset.GIF ID = 1000000034&安培; EXT = GIF
以下是我尝试将此GIF推送到NSData的两种方法,每次myImageData显示(null)。
我尝试过使用initWithContentsOfURL:
NSData *dataFromGIFURL = [[NSData alloc] initWithContentsOfURL: myGIFURL];
self.myImageData = dataFromGIFURL;
[dataFromGIFURL release];
然后我尝试将NSURL转换为initWithContentsOfFile的字符串:
NSString *stringFromURL = [NSString stringWithFormat:@"%@", myGIFURL];
NSData *dataFromGIFURL = [[NSData alloc] initWithContentsOfFile: stringFromURL];
self.myImageData = dataFromGIFURL;
[dataFromGIFURL release];
有什么建议吗?感谢。
答案 0 :(得分:13)
UIImagePickerControllerReferenceURL键在iOS 4.1之前不会出现。因此,我在你的问题中暗示,使用AssetsLibrary框架是很好的,该框架仅在4.0版本的iOS中出现。在这种情况下,您可以使用以下内容:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *asset)
{
ALAssetRepresentation *representation = [asset defaultRepresentation];
NSLog(@"size of asset in bytes: %d", [representation size]);
unsigned char bytes[4];
[representation getBytes:bytes fromOffset:0 length:4 error:nil];
NSLog(@"first four bytes: %02x (%c) %02x (%c) %02x (%c) %02x (%c)",
bytes[0], bytes[0],
bytes[1], bytes[1],
bytes[2], bytes[2],
bytes[3], bytes[3]);
[library autorelease];
}
failureBlock:^(NSError *error)
{
NSLog(@"couldn't get asset: %@", error);
[library autorelease];
}
];
}
所以,你创建一个ALAssetsLibrary,让它找到你指定URL的资产(它理解assets-library:// URL方案),然后当你获得资产时,你获取它的默认表示并使用它来喂你的字节。它们将是实际的磁盘上字节,来自磁带库的资产的默认表示形式是磁盘形式。
例如,选择我从Google图片中随机抓取的特定GIF,从连接到使用该方法的委托的图像选择器中为我提供输出:
2011-03-03 23:17:37.451 IPTest [1199:307]资产规模 字节:174960
2011-03-03 23:17:37.459 IPTest [1199:307]前四个字节:47 (G)49(I)46(F)38(8)
这就是标准GIF标题的开头。选择PNG或JPG可以识别PNG和JPG标题的前四个字节。
编辑:为了完成这个想法,显然你可以使用ALAssetRepresentation将描述文件的所有字节读入一个适当的malloc'd C数组,然后使用NSData +(id)dataWithBytes:length :(或者更可能的话, + dataWithBytesNoCopy:length:freeWhenDone :)将它包装成NSData。答案 1 :(得分:6)
这是一个使用较新的Photos框架的版本:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL * refUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
if (refUrl) {
PHAsset * asset = [[PHAsset fetchAssetsWithALAssetURLs:@[refUrl] options:nil] lastObject];
if (asset) {
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
options.networkAccessAllowed = NO;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSNumber * isError = [info objectForKey:PHImageErrorKey];
NSNumber * isCloud = [info objectForKey:PHImageResultIsInCloudKey];
if ([isError boolValue] || [isCloud boolValue] || ! imageData) {
// fail
} else {
// success, data is in imageData
}
}];
}
}
}
答案 2 :(得分:4)
这是使用Swift 3的Eli版本:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
guard let imageURL = info[UIImagePickerControllerReferenceURL] as? URL else { return }
guard let asset = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil).lastObject else { return }
if picker.sourceType == .photoLibrary || picker.sourceType == .savedPhotosAlbum {
let options = PHImageRequestOptions()
options.isSynchronous = true
options.isNetworkAccessAllowed = false
options.deliveryMode = .highQualityFormat
PHImageManager.default().requestImageData(for: asset, options: options) { data, uti, orientation, info in
guard let info = info else { return }
if let error = info[PHImageErrorKey] as? Error {
log.error("Cannot fetch data for GIF image: \(error)")
return
}
if let isInCould = info[PHImageResultIsInCloudKey] as? Bool, isInCould {
log.error("Cannot fetch data from cloud. Option for network access not set.")
return
}
// do something with data (it is a Data object)
}
} else {
// do something with media taken via camera
}
}
答案 3 :(得分:-1)
迅速4更新:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! CFString
let assetPath = info[UIImagePickerControllerReferenceURL] as! URL
self.dismiss(animated: true, completion: nil)
switch mediaType {
case kUTTypeImage, kUTTypeLivePhoto, kUTTypeGIF:
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
if (assetPath.absoluteString.hasSuffix("GIF")) || (assetPath.absoluteString.hasSuffix("gif")){
print("GIF")
let options = PHImageRequestOptions()
options.isSynchronous = true
options.isNetworkAccessAllowed = false
options.deliveryMode = .highQualityFormat
guard let asset = PHAsset.fetchAssets(withALAssetURLs: [assetPath], options: nil).lastObject else { return }
PHImageManager.default().requestImageData(for: asset, options: options) { data, uti, orientation, info in
guard let info = info else { return }
if let error = info[PHImageErrorKey] as? Error {
print("Cannot fetch data for GIF image: \(error)")
return
}
if let isInCould = info[PHImageResultIsInCloudKey] as? Bool, isInCould {
print("Cannot fetch data from cloud. Option for network access not set.")
return
}
if let gifImageData = data {
// do something with data (it is a Data object)
}
}
} else {
// this is the basic image
}
}
case kUTTypeMovie:
if let videoURL = info[UIImagePickerControllerMediaURL] as? URL {
// using video type
}
default:
print("Others")
}
}