Cropping an Image to the shape of an Overlay - iOS

前端 未结 3 547
悲哀的现实
悲哀的现实 2021-02-14 17:27

I\'m adding the overlay using pickerController\'s view and the uinavigationcontrollerdelegate as below.

-(void)navigationController:(UINavigationController *)nav         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-14 17:36

    Im having exactly the same profile image picker controller as your. Here is my code from delegate. I don't think you need everything but you can find some useful information here

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        NSString *mediaType = info[UIImagePickerControllerMediaType];
    
        if([mediaType isEqualToString:(NSString *) kUTTypeImage]) {
    
            UIImage *image = info[UIImagePickerControllerOriginalImage];
            UIImage *editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
            CGRect croppingRect = [info[UIImagePickerControllerCropRect] CGRectValue];
    
            if (editedImage) {
                image = editedImage;
            } else {
                CGFloat smaller = 1;
                if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
                    smaller = 0.9;
                }
    
                CGFloat width = MIN(image.size.width * smaller, image.size.height * (smaller * 0.95));
                croppingRect = CGRectMake(0 + (image.size.width - width) / 2,
                                          0 + (image.size.height - width) / 2,
                                          width, width);
            }
    
            UIImage *finalImage = nil;
            if (editedImage) {
                finalImage = [UIImage image:editedImage byScalingAndCroppingForSize:kCroppedImageSize];
            } else {
                finalImage = [UIImage image:image byScalingAndCroppingForSize:kCroppedImageSize];
            }
    
            if ([self.imagePickerDelegate respondsToSelector:@selector(profileImagePicker:didSelectImage:)]) {
                [self.imagePickerDelegate profileImagePicker:self didSelectImage:finalImage];
            } else {
                NSAssert(nil, @"Delegate should confirm ProfileImagePickerControllerDelegate protocol");
            }
    
        } else if ([mediaType isEqualToString:(NSString *) kUTTypeVideo]) {
            NSAssert(nil, @"Movie is not supported");
        }
    }
    

提交回复
热议问题