0
我想製作一個簡單的應用程序,它將相機覆蓋圖與在UIImagePicker中拍攝的照片相結合。在這個例子中,我想將熊耳朵的疊加視圖與照片結合起來。無法使用UIGraphics合併兩個圖像,圖像位置不正確
- (IBAction)pushTakePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
//create overlay UIView
UIImage *bearEars = [UIImage imageNamed:@"bearEars"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:bearEars];
UIView *camerOverlayView = [[UIView alloc] initWithFrame:imageView.frame];
[camerOverlayView addSubview:imageView];
[picker setCameraOverlayView:camerOverlayView];
[self presentViewController:picker animated:YES completion:NULL];
}
上面的代碼創建覆蓋,這工作正常。下面的代碼組合所述圖像:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
//first we flip the image
UIImage * flippedImage = [UIImage imageWithCGImage:chosenImage.CGImage scale:chosenImage.scale orientation:UIImageOrientationLeftMirrored];
chosenImage=flippedImage;
//now we need to add the ears
//get pics
UIImage *backgroundImage = chosenImage;
UIImage *watermarkImage = [UIImage imageNamed:@"bearEars"];
//start a workspace
UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
//position seems off for some reason
[watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
chosenImage=result;
self.finalImageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
當我結合然而bearEars覆蓋不位於(0,0),但在代替的最終圖像的中間圖像。我不知道爲什麼會發生這種情況。我只是想要它像覆蓋相機視圖中的圖像。任何想法爲什麼發生這種情況?謝謝
我試過了,它只是使watermarkImage小得多,而不是正確的大小 – Kex 2015-02-07 09:41:19
我已經擴大了答案,希望它有幫助 – 2015-02-07 10:38:59
嘿這個作品,現在有道理。謝謝! – Kex 2015-02-07 11:44:15