UIView 를 이미지로 만들고 유저의 사진앨범에 저장합니다.

  • 사진앨범에 저장할 경우, 이미지 파일명을 정할 수 없습니다.
  • 이미지를 자르는 경우, image size 와 scale 에 주의해야 합니다.
  • UIView에서 보이는 것들만 저장됩니다.
CGFloat scale = [UIScreen mainScreen].scale;

/// 이미지 만들기.
// Creates a bitmap-based graphics context
UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, scale);
// Renders the layer and its sublayers into the specified context.
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Returns an image based on the contents of the current bitmap-based graphics context.
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Removes the current bitmap-based graphics context from the top of the stack.
UIGraphicsEndImageContext();

/// 이미지 자르기.
CGRect imgRect = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);
// Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
CGImageRef croppedImgRef = CGImageCreateWithImageInRect(image.CGImage, imgRect);
// Creates and returns an image object with the specified scale and orientation factors.
UIImage *croppedImage = [UIImage imageWithCGImage:croppedImgRef scale:scale orientation:UIImageOrientationUp];
// Decrements the retain count of a bitmap image.
CGImageRelease(croppedImgRef);

// Adds the specified image to the user’s Camera Roll album.
UIImageWriteToSavedPhotosAlbum(croppedImage, self, @selector(didSaveReceiptImage:didFinishSavingWithError:contextInfo:), nil);

+ Recent posts