iOS 자체 워터마크 이미지

1976 단어

1. 배경 그림 UIImage 객체 만들기

UIImage *bgImage = [UIImage imageNamed:@"bgImage"];

2, 비트맵 컨텍스트 만들기


UIGraphicsBeginImageContextWithOptions(size,opaque,scale); 크기: 그림 크기 opaque: YES 불투명, NO 투명 scale: 그림을 생성하는 크기 가져오기: 2020 그림 크기 생성: (20scale20scale

3. 배경 그림을 그림에 추가하고drawInRect를 호출하는 방법:


4. 워터마크 이미지 객체를 만들고 drawInRect를 호출하는 방법: 이를 그림 상하문에 추가하는 방법


5, 비트맵 컨텍스트에서 작성된 새 이미지를 가져오고 컨트롤러의 이미지 뷰에 표시하며 비트맵 컨텍스트에서 작성된 새 이미지를 가져오는 방법 UIGraphicsGetImageFromCurrentImageContext()


6, 비트맵 편집 끝내기, UIGraphicsEndImageContext 호출()으로 편집 끝내기


7、합성된 그림을 저장


그림을 NSData로 변환하고 NSData의 write 방법을 호출합니다.그 중에서 UIImage를 NSData로 변환하는 방법은 두 가지가 있는데 그것이 바로 UIImagePNGrepresentation()과 UIImageJPEGrepresentation()이다.

부분 코드:

    UIImage *bgImage = [UIImage imageNamed:@"bgImage"];
    // UIImage , imageView 
    // , 
    //size  
    //opaque alpha YES  , NO 
    //scale    :20*20  :(20*scale*20*scale)
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 4);
    
    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.width)];
    
    UIImage *waterImage = [UIImage imageNamed:@"header"];
    [waterImage drawInRect:CGRectMake(bgImage.size.width - 60, bgImage.size.height - 60, 60, 60)];
    // 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 
    UIGraphicsEndImageContext();
    
    self.imageView.image = newImage;
    
    //  Image png
    //  UIImage NSData ,NSData write 
    NSData *iamgeData = UIImagePNGRepresentation(newImage);
    [iamgeData writeToFile:@"/Users/apple/Desktop/new.png" atomically:YES];
    
    //  jpg
    //compressionQuality    (0-1)
    // 100KB * 0.7 = 70KB
    NSData *imageData = UIImageJPEGRepresentation(newImage, 0.7);
    [imageData writeToFile:@"/Users/apple/Desktop/new1.jpg" atomically:YES];

좋은 웹페이지 즐겨찾기