I am trying to load a jpg
image together with a mov
file with objective-c on ios device to display a live photo, and I make following code snippet to do that in viewDidLoad
function:
- (void)viewDidLoad {
[super viewDidLoad];
PHLivePhotoView *photoView = [[PHLivePhotoView alloc]initWithFrame:self.view.bounds];
NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"livePhoto" withExtension:@"jpg"];
NSURL *videoUrl = [[NSBundle mainBundle] URLForResource:@"livePhoto" withExtension:@"mov"];
[PHLivePhoto requestLivePhotoWithResourceFileURLs:@[videoUrl, imageUrl] placeholderImage:[UIImage imageNamed:@"livePhoto.jpg"] targetSize:self.view.bounds.size contentMode:PHImageContentModeAspectFit resultHandler:^(PHLivePhoto *livePhoto, NSDictionary *info){
NSLog(@"we are in handler");
photoView.livePhoto = livePhoto;
photoView.contentMode = UIViewContentModeScaleAspectFit;
photoView.tag = 87;
[self.view addSubview:photoView];
[self.view sendSubviewToBack:photoView];
}];
}
I have drag the file livePhoto.jpg
and livePhoto.mov
to Xcode project
But when build this Xcode log this error:
2017-11-28 17:46:08.568455+0800 Live Photos[3669:1276778] we are in handler
2017-11-28 17:46:08.580439+0800 Live Photos[3669:1276778] we are in handler
2017-11-28 17:46:08.597147+0800 Live Photos[3669:1276806] Error: Invalid image metadata
2017-11-28 17:46:08.607881+0800 Live Photos[3669:1276806] Error: Invalid video metadata
2017-11-28 17:46:08.608329+0800 Live Photos[3669:1276778] we are in handler
Any idea about that? Thanks.
And another thing to ask:
Why does the resultHandler
was called twice according to what is printed?
TL;DR
Here's the code to store Live Photos and upload them to a server:
1. Capturing Live Photo
expectedAsset
is just an object holding all required information. You can use a NSDictionary instead. And since this code snippet is a >= iOS 11 API, heres the one for "deprecated" iOS...2. Generate NSData
Long Answer
This is caused by wrong Metadata in the video/image file. When creating a live photo, PHLivePhoto searches for the key 17 in
kCGImagePropertyMakerAppleDictionary
(which is the asset identifier) and matches this with thecom.apple.quicktime.content.identifier
of the mov file. The mov file also needs to have an entry for the time where the still image was captured (com.apple.quicktime.still-image-time
).Make sure your files haven't been edited (or exported) somewhere. Event the UIImageJPEGRepresentation function will remove this data from the image.
Here's a code snippet I'm using to convert the UIImage to NSData:
The Handler gets called twice to first tell you about corrupt data, and the second time about the cancellation of the process (these are two different keys).
EDIT:
Here's your mov data:
The
com.apple.quicktime.still-image-time
key is missing here.Here's the metadata how it should look like:
And just FYI, heres your JPEG Data: