Fix ability to attach photos from camera (#1112)

* Fix ability to attach photos from camera

Looks like this came about with the animated GIF handling. We'll only
go down the byte-comparison-mime-type-detecting code path for attaching
existing photos, since it only exists for animated GIFs.

This will also revert to properly compressing our image attachments, so
long as they are taken from the camera.

* Prevent crash when tapping broken image

It never makes sense to present a "full screen" nil image. Previously
this happened when camera-capture was broken, but could conceivably
happen for other as-of-yet unknown reasons.

// FREEBIE
pull/1/head
Michael Kirk 10 years ago
parent c0bb704d2d
commit 5869fb8e06

@ -931,50 +931,54 @@ typedef enum : NSUInteger {
if ([[messageItem media] isKindOfClass:[TSPhotoAdapter class]]) { if ([[messageItem media] isKindOfClass:[TSPhotoAdapter class]]) {
TSPhotoAdapter *messageMedia = (TSPhotoAdapter *)[messageItem media]; TSPhotoAdapter *messageMedia = (TSPhotoAdapter *)[messageItem media];
if ([messageMedia isImage]) { tappedImage = ((UIImageView *)[messageMedia mediaView]).image;
tappedImage = ((UIImageView *)[messageMedia mediaView]).image; if(tappedImage == nil) {
DDLogWarn(@"tapped TSPhotoAdapter with nil image");
} else {
CGRect convertedRect = CGRect convertedRect =
[self.collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame [self.collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame
toView:nil]; toView:nil];
__block TSAttachment *attachment = nil; __block TSAttachment *attachment = nil;
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
attachment = attachment =
[TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction]; [TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction];
}]; }];
if ([attachment isKindOfClass:[TSAttachmentStream class]]) { if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
TSAttachmentStream *attStream = (TSAttachmentStream *)attachment; TSAttachmentStream *attStream = (TSAttachmentStream *)attachment;
FullImageViewController *vc = [[FullImageViewController alloc] FullImageViewController *vc = [[FullImageViewController alloc]
initWithAttachment:attStream initWithAttachment:attStream
fromRect:convertedRect fromRect:convertedRect
forInteraction:[self interactionAtIndexPath:indexPath] forInteraction:[self interactionAtIndexPath:indexPath]
isAnimated:NO]; isAnimated:NO];
[vc presentFromViewController:self.navigationController]; [vc presentFromViewController:self.navigationController];
} }
} else {
DDLogWarn(@"Currently unsupported");
} }
} else if ([[messageItem media] isKindOfClass:[TSAnimatedAdapter class]]) { } else if ([[messageItem media] isKindOfClass:[TSAnimatedAdapter class]]) {
// Show animated image full-screen // Show animated image full-screen
TSAnimatedAdapter *messageMedia = (TSAnimatedAdapter *)[messageItem media]; TSAnimatedAdapter *messageMedia = (TSAnimatedAdapter *)[messageItem media];
tappedImage = ((UIImageView *)[messageMedia mediaView]).image; tappedImage = ((UIImageView *)[messageMedia mediaView]).image;
CGRect convertedRect = if(tappedImage == nil) {
DDLogWarn(@"tapped TSAnimatedAdapter with nil image");
} else {
CGRect convertedRect =
[self.collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame [self.collectionView convertRect:[collectionView cellForItemAtIndexPath:indexPath].frame
toView:nil]; toView:nil];
__block TSAttachment *attachment = nil; __block TSAttachment *attachment = nil;
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
attachment = attachment =
[TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction]; [TSAttachment fetchObjectWithUniqueID:messageMedia.attachmentId transaction:transaction];
}]; }];
if ([attachment isKindOfClass:[TSAttachmentStream class]]) { if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
TSAttachmentStream *attStream = (TSAttachmentStream *)attachment; TSAttachmentStream *attStream = (TSAttachmentStream *)attachment;
FullImageViewController *vc = FullImageViewController *vc =
[[FullImageViewController alloc] initWithAttachment:attStream [[FullImageViewController alloc] initWithAttachment:attStream
fromRect:convertedRect fromRect:convertedRect
forInteraction:[self interactionAtIndexPath:indexPath] forInteraction:[self interactionAtIndexPath:indexPath]
isAnimated:YES]; isAnimated:YES];
[vc presentFromViewController:self.navigationController]; [vc presentFromViewController:self.navigationController];
}
} }
} else if ([[messageItem media] isKindOfClass:[TSVideoAttachmentAdapter class]]) { } else if ([[messageItem media] isKindOfClass:[TSVideoAttachmentAdapter class]]) {
// fileurl disappeared should look up in db as before. will do refactor // fileurl disappeared should look up in db as before. will do refactor
@ -1364,7 +1368,7 @@ typedef enum : NSUInteger {
/* /*
* Fetching data from UIImagePickerController * Fetching data from UIImagePickerController
*/ */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
[UIUtil modalCompletionBlock](); [UIUtil modalCompletionBlock]();
[self resetFrame]; [self resetFrame];
@ -1373,45 +1377,56 @@ typedef enum : NSUInteger {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
[self sendQualityAdjustedAttachment:videoURL]; [self sendQualityAdjustedAttachment:videoURL];
} else { } else {
// Send image as NSData to accommodate both static and animated images if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; {
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] // Image captured from camera
resultBlock:^(ALAsset *asset) { UIImage *pictureCamera = [[info objectForKey:UIImagePickerControllerOriginalImage] normalizedImage];
ALAssetRepresentation *representation = [asset defaultRepresentation]; if (pictureCamera) {
Byte *img_buffer = (Byte *)malloc((unsigned long)representation.size); DDLogVerbose(@"Sending picture attachement ...");
NSUInteger length_buffered = [self sendMessageAttachment:[self qualityAdjustedAttachmentForImage:pictureCamera] ofType:@"image/jpeg"];
[representation getBytes:img_buffer fromOffset:0 length:(unsigned long)representation.size error:nil];
NSData *img_data = [NSData dataWithBytesNoCopy:img_buffer length:length_buffered];
NSString *file_type;
switch (img_buffer[0]) {
case 0x89:
file_type = @"image/png";
break;
case 0x47:
file_type = @"image/gif";
break;
case 0x49:
case 0x4D:
file_type = @"image/tiff";
break;
case 0x42:
file_type = @"@image/bmp";
break;
case 0xFF:
default:
file_type = @"image/jpeg";
break;
}
DDLogVerbose(@"Sending image. Size in bytes: %lu; first byte: %02x (%c); detected filetype: %@",
(unsigned long)length_buffered,
img_buffer[0],
img_buffer[0],
file_type);
[self sendMessageAttachment:img_data ofType:file_type];
} }
failureBlock:^(NSError *error) { } else {
DDLogVerbose(@"Couldn't get image asset: %@", error); // Image picked from library
}]; // Send image as NSData to accommodate both static and animated images
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *representation = [asset defaultRepresentation];
Byte *img_buffer = (Byte *)malloc((unsigned long)representation.size);
NSUInteger length_buffered =
[representation getBytes:img_buffer fromOffset:0 length:(unsigned long)representation.size error:nil];
NSData *img_data = [NSData dataWithBytesNoCopy:img_buffer length:length_buffered];
NSString *file_type;
switch (img_buffer[0]) {
case 0x89:
file_type = @"image/png";
break;
case 0x47:
file_type = @"image/gif";
break;
case 0x49:
case 0x4D:
file_type = @"image/tiff";
break;
case 0x42:
file_type = @"@image/bmp";
break;
case 0xFF:
default:
file_type = @"image/jpeg";
break;
}
DDLogVerbose(@"Sending image. Size in bytes: %lu; first byte: %02x (%c); detected filetype: %@",
(unsigned long)length_buffered,
img_buffer[0],
img_buffer[0],
file_type);
[self sendMessageAttachment:img_data ofType:file_type];
}
failureBlock:^(NSError *error) {
DDLogVerbose(@"Couldn't get image asset: %@", error);
}];
}
} }
} }

Loading…
Cancel
Save