Getting NSData from UIImage object that contains CGImage

拜拜、爱过 提交于 2019-12-06 05:44:21

Better answer:

You need to truly set a UIImage object (and not a CGImage pointer) to a property that you've declared to be a UIImage object.

Original:

if "finalPhotoToSend" is a property, you should be doing:

NSData *imageData = UIImageJPEGRepresentation(self.finalPhotoToSend, 0.7);

and not:

NSData *imageData = UIImageJPEGRepresentation(finalPhotoToSend, 0.7);

I just figured it out. Here's what I ended up using for the IBAction method implementation:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:self {


    if ([segue.identifier isEqualToString:@"mediaCaptureToSendToFriendsSegue"]) {


        SendToFriendsViewController *sendFriendsVC = segue.destinationViewController;


        UIImage* image2 = [UIImage imageWithCGImage:(__bridge CGImageRef)(_subLayer.contents)];

        sendFriendsVC.finalPhotoToSend = image2;


    }
}

I'm new to objective-c and not really entirely sure what the following statement is even doing:

UIImage* image2 = [UIImage imageWithCGImage:(__bridge CGImageRef)(_subLayer.contents)];

But xcode suggested it and it works. Now on the next view controller, the finalPhotoToSend object NSLogs as a UIImage instead of a CGImage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!